Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and ninclusive, and the two sequences were not identical, that is, at least one i (1 ≤ i ≤ n) exists, such that ai ≠ bi holds.
Well, she almost had it all — each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 ≤ i ≤ n) such that ai ≠ pi, and exactly one j (1 ≤ j ≤ n) such that bj ≠ pj.
For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.
The first line of input contains a positive integer n (2 ≤ n ≤ 1 000) — the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of colours in the first meteor outburst.
The third line contains n space-separated integers b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of colours in the second meteor outburst. At least one i (1 ≤ i ≤ n) exists, such that ai ≠ bi holds.
Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists.
5 1 2 3 4 3 1 2 5 4 5
1 2 5 4 3
5 4 4 2 3 1 5 4 5 3 1
5 4 2 3 1
4 1 1 3 4 1 4 3 4
1 2 3 4
In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.
In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints.
題目大意爲,給一個n,然後輸入兩組n個數的數據,讓你求出一組數(這組數中1-n每個數只能用一次),這組數與第一組數有且只有一個數不同,與第二組也是有且只有一個不同,而且,1,2組數據也不同。如果能有多種答案,任意一個答案輸出即可。
從測試數據即可看出,有兩種情況,第一種:就是第三組與第一組的數不同的下標和第三組與第二組不同的數的下標不同的時候(因爲有且只有一個不同,所以可以先記錄下這兩個下標x,y,並把這四個數a[x],a[y],b[x],b[y]提取出來,然後再用一個C數組存除了這兩個下標之外的數的出現,因爲第三組與第一二組都一個不同的,那麼肯定是在a[x],a[y],b[x],b[y]中的對角線即選擇a[x],b[y]一組或者a[y],b[x],並且這兩個數必須是隻能出現一次,判斷一次就好這,第一個不行那麼就是第二個。沒有其他的情況。然後讓這兩個數頂替原來這兩個下標的數,最後輸出即可。
第二種:也就是第三組與第一二組的不同的數的下標一樣,同上一樣將其他下標的數存入C數組中,然後從1開始找哪個數沒有出現過,那麼這個數就是第三組這個下標位置的數,然後將這個數放入這個下標的數,輸出序列數組即可。
代碼也是比較簡單明瞭哈哈哈哈哈哈~_~
只可惜這個題昨天晚上比賽的時候沒有做出來,今天看了測試數據,自己犯了一個很低級的錯誤。╮(╯▽╰)╭,第一次比賽就這麼過去了,好傷哦-----
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[1001],b[1001],c[1001];
int n;
scanf("%d",&n);
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
int x,y;
int t=1;
x=0;
y=0;
for(int i=1;i<=n;i++)
{
if((a[i]!=b[i])&&t==1)
{
x=i;
t=0;
continue;
}
if(a[i]!=b[i]&&t==0)
{
y=i;
}
}
for(int i=1;i<=n;i++)
{
if(i==x||i==y)
continue;
else
c[a[i]]++;
}
if(y==0)
{
for(int i=1;i<=n;i++)
{
if(c[i]==0)
{
a[x]=i;
break;
}
}
for(int i=1;i<=n;i++)
{
if(i==1)
printf("%d",a[i]);
else
printf(" %d",a[i]);
}
}
else
{
c[a[x]]++;
c[b[y]]++;
if(c[a[x]]==1&&(c[a[x]]==c[b[y]]))
{
a[y]=b[y];
}
else
a[x]=b[x];
for(int i=1;i<=n;i++)
{
if(i==1)
printf("%d",a[i]);
else
printf(" %d",a[i]);
}
}
return 0;
}