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.
hint:题目给你两个长为n的序列,两个序列至少有一个元素不同,问你能不能用1-n n个数字构造出一个序列使得这个序列和给出的两个序列恰好有一个元素不相同。我们冷静分析一下可以发现,所给的两个中共有的元素一定要出现在我们构造的序列的对应位置,而且所给的两个序列最多只有两对数不同(可以简单证明一下),所以我们针对两个序列有一对数不同和两个序列有两队数不同进行讨论一下,这里其实乱搞的话要细心一点。。。(连我自己都不知道自己的代码为什么是对的。。。)看题解好像也是,多判断几种情况暴力乱搞。。
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<string> 5 #include<vector> 6 #include<stack> 7 #include<queue> 8 #include<string.h> 9 #include<stdlib.h> 10 using namespace std; 11 typedef long long ll; 12 int a[1050], b[1050], vis[1050], c[1050]; 13 14 //智商不够用。。。多加几个判断,考虑情况要全面一点。。。 15 int main(){ 16 int n; 17 memset(vis, 0, sizeof(vis)); 18 memset(c, 0, sizeof(c)); 19 memset(a, 0, sizeof(a)); 20 memset(b, 0, sizeof(b)); 21 cin >> n; 22 for(int i=1; i<=n; i++){ 23 cin >> a[i]; 24 } 25 for(int i=1; i<=n; i++){ 26 cin >> b[i]; 27 } 28 int cnt = 0; 29 for(int i=1; i<=n; i++){ 30 if(a[i] == b[i]){ 31 cnt++; 32 c[i] = a[i]; 33 vis[a[i]] = 1; 34 } 35 } 36 if(n - cnt == 1){ 37 int key; 38 for(int i=1; i<=n; i++){ 39 if(!vis[i]){ 40 key = i; 41 } 42 } 43 for(int i=1; i<=n; i++){ 44 if(c[i] == 0) cout << key << " "; 45 else cout << c[i] <<" "; 46 } 47 }else if(n - cnt == 2){ 48 int key[2], x=0, index[2]; 49 for(int i=1; i<=n; i++){ 50 if(!vis[i]){ 51 key[x++] = i; 52 } 53 } 54 x = 0; 55 for(int i=1; i<=n; i++){ 56 if(a[i] != b[i]) index[x++] = i; 57 } 58 for(int i=1; i<=n; i++){ 59 if(c[i] == 0){ 60 if(!vis[b[i]] && !vis[a[i]]){ 61 if(index[0] == i){ 62 if(vis[a[index[1]]]) { 63 cout << a[i] << " "; 64 vis[a[i]] = 1; 65 } 66 else{ 67 cout << b[i] << " "; 68 vis[b[i]] = 1; 69 } 70 }else{ 71 if(vis[a[index[0]]]){ 72 cout << a[i] << " "; 73 vis[a[i]] = 1; 74 } 75 else{ 76 cout << b[i] << " "; 77 vis[b[i]] = 1; 78 } 79 } 80 } 81 else if(!vis[b[i]]){ 82 cout << b[i] << " "; 83 vis[b[i]] = 1; 84 }else if(!vis[a[i]]){ 85 cout << a[i] << " "; 86 vis[a[i]] = 1; 87 } 88 } 89 else cout << c[i] << " "; 90 } 91 } 92 return 0; 93 }