Codeforces Round #418 (Div. 2) B

Description

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 n inclusive, 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.

Input

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

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.

Examples
input
5
1 2 3 4 3
1 2 5 4 5
output
1 2 5 4 3
input
5
4 4 2 3 1
5 4 5 3 1
output
5 4 2 3 1
input
4
1 1 3 4
1 4 3 4
output
1 2 3 4
Note

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.

题意:连蒙带猜,大概说的是用两个序列还原出正确的序列,1-n各出现一次

解法:(读代码可能好懂许多)首先相同数字出现的一定要保存,然后不同的数字判断这两个数字以后会不会一定出现(即当中一个数字以后会在相同位置出现),出现过的以后不能再出现

于是。。。得到初始序列,不过这个序列还需要满足1-n各出现一次,所以还要保存未出现的数字,接下来就是按照题意规定在判断输出一次

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 const int maxn=123456;
 5 int a,b,c;
 6 int x[maxn],y[maxn],z[maxn];
 7 vector<int>q,p;
 8 map<int,int>m,n,s,k;
 9 int main()
10 {
11     cin>>a;
12     for(int i=1;i<=a;i++)
13     {
14         cin>>x[i];
15     }
16     for(int i=1;i<=a;i++)
17     {
18         cin>>y[i];
19     }
20     for(int i=1;i<=a;i++)
21     {
22         if(x[i]==y[i])
23         {
24             s[x[i]]=1;
25         }
26 
27     }
28     for(int i=1;i<=a;i++)
29     {
30      //   cout<<s[x[i]]<<" ";
31         if(x[i]!=y[i])
32         {
33             if(s[x[i]]==1)//相同位置不同数字,但x[i]在后面必须出现,选y[i]
34             {
35                 k[y[i]]++;
36                 q.push_back(y[i]);
37             }
38             else if(s[y[i]]==1)//相同位置不同数字,但y[i]在后面必须出现,选x[i]
39             {
40                 k[x[i]]++;
41                 q.push_back(x[i]);
42             }
43             else
44             {
45                 if(k[x[i]]==0)
46                 {
47                     k[x[i]]++;
48                     q.push_back(x[i]);
49                 }
50                 else if(k[y[i]]==0)
51                 {
52                     k[y[i]]++;
53                     q.push_back(y[i]);
54                 }
55             }
56         }
57         else
58         {
59             k[x[i]]++;
60             q.push_back(x[i]);
61         }
62     }
63     for(int i=0;i<q.size();i++)
64     {
65         z[q[i]]++;
66     }
67     for(int i=1;i<=a;i++)
68     {
69         if(z[i]==0)
70         {
71             p.push_back(i);
72         }
73     }
74     int ans=0;
75     map<int,int>o;
76     for(int i=0;i<q.size();i++)
77     {
78         if(x[i+1]==y[i+1])
79         {
80           //  z[x[i+1]]--;
81             cout<<x[i+1]<<" ";
82         }
83         else if(z[q[i]]==1)
84         {
85             cout<<q[i]<<" ";
86         }
87         else
88         {
89             z[q[i]]--;
90             cout<<p[ans++]<<" ";
91         }
92     }
93     return 0;
94 }
原文地址:https://www.cnblogs.com/yinghualuowu/p/6962091.html