Codeforces Round #418 B--An express train to reveries

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.

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

51 2 3 4 31 2 5 4 5

output

1 2 5 4 3

input

54 4 2 3 15 4 5 3 1

output

5 4 2 3 1

input

41 1 3 41 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.

题意:

有一个 111 至 nnn 的所有整数形成的排列 p1,p2,…,pnp_1, p_2, \ldots, p_np?1??,p?2??,…,p?n??。

有两个长度为 nnn 的数组 a1,a2,…,ana_1, a_2, \ldots, a_na?1??,a?2??,…,a?n?? 和 b1,b2,…,bnb_1, b_2, \ldots, b_nb?1??,b?2??,…,b?n??。它们分别有恰好 n−1n - 1n−1 个位置上的元素与 ppp 相同,即存在恰好一个 iii(1≤i≤n1 \leq i \leq n1≤i≤n)使得 ai≠pia_i \neq p_ia?i??≠p?i??,存在恰好一个 jjj(1≤j≤n1 \leq j \leq n1≤j≤n)使得 bj≠pjb_j \neq p_jb?j??≠p?j??。另外,aaa 与 bbb 不相同,即存在至少一个 iii(1≤i≤n1 \leq i \leq n1≤i≤n)使得 ai≠bia_i \neq b_ia?i??≠b?i??。

请给出任意一个满足条件的排列 ppp。输入保证这样的排列存在。

题意说的很清楚,求一个与a和b只相差一个数的序列

只是注意 1-n中的数要全出现,每个数只能用一次

 1 #include <cctype>
 2 #include <cstdio>
 3
 4 const int MAXN=1010;
 5
 6 int n;
 7
 8 int a[MAXN],b[MAXN],p[MAXN],vis[MAXN],q[MAXN];
 9
10 inline void read(int&x) {
11     int f=1;register char c=getchar();
12     for(x=0;!isdigit(c);c==‘-‘&&(f=-1),c=getchar());
13     for(;isdigit(c);x=x*10+c-48,c=getchar());
14     x=x*f;
15 }
16
17 int hh() {
18     int t=0;
19     read(n);
20     for(int i=1;i<=n;++i) read(a[i]);
21     for(int j=1;j<=n;++j) read(b[j]);
22     for(int i=1;i<=n;++i) {
23         p[i]=a[i],++vis[a[i]];
24         if(a[i]!=b[i]) q[++t]=i;
25      }
26     for(int i=1;i<=t;++i) {
27         if(vis[a[q[i]]]>1) {
28             for(int j=1;j<=n;++j) {
29                 if(!vis[j]) {
30                     if(t>1&&j!=b[q[i]]) break;
31                     p[q[i]]=j;
32                     goto NEXT;
33                 }
34             }
35         }
36     }
37 NEXT:
38     for(int i=1;i<=n;++i) printf("%d ",p[i]);
39     return 0;
40 }
41
42 int sb=hh();
43 int main(int argc,char**argv) {;}

代码

时间: 2024-10-12 23:21:47

Codeforces Round #418 B--An express train to reveries的相关文章

loj #6091. 「Codeforces Round #418」幻想特快

#6091. 「Codeforces Round #418」幻想特快 1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdio> 5 #include<queue> 6 using namespace std; 7 #define maxn 10000 8 int n,a[maxn],b[maxn],p[maxn],nxt1,nxt2,tot,

Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection(双指针)

题目链接:Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection 题意: 给你一个字符串,有q个询问,每个询问一个x和一个字符 o. 现在让你在原来的字符串上最多改变x个字符,问能构成最长的o子串的长度. 题解: 一共有26*1500种状态,对于每个状态用双指针滚一滚就行了. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=(a);i<

Codeforces Round #418 B

An express train to reveries 题意:给2长度为n的数列a b,a b中至少存在一个i使得ai!=bi,构造一个数列p,使得恰好存在一个i使得ai!=pi,且恰好存在一个j使得aj!=pj,且p里面的数为1-n且无重复,输出p,保证有解 思路:题目保证有解,可知,最多存在2个i使得ai!=bi,最少一个,如果只有一个,那么好办,令p=a,将pi换成1-n中没有出现的那个数就可以了,如果有存在2个不等的数,那么令p=a(除开两个ai!=bi的位置),找出未出现的2个数,将

codeforces round 418 div2 补题 CF 814 A-E

A An abandoned sentiment from past 水题 #include<bits/stdc++.h> using namespace std; int a[300],b[300],n,k; bool cmp(int a,int b) { return a>b; } int main() {//freopen("t.txt","r",stdin); scanf("%d%d",&n,&k); f

Codeforces Round #418 (Div. 2)

A - An abandoned sentiment from past 由于所有的数组B内所有的数都不同,因此当k > 1是就可以使该序列不递增 当k = 1是,带入B[0],判断序列A是否递增就可以啦 #include <bits/stdc++.h> using namespace std; const int MAXN = 100008; int a[MAXN], b[MAXN]; int main() { int n, k; scanf("%d%d", &am

Codeforces - 814B - An express train to reveries - 构造

http://codeforces.com/problemset/problem/814/B 构造题烦死人,一开始我还记录一大堆信息来构造p数列,其实因为s数列只有两项相等,也正好缺了一项,那就把两种情况构造出来暴力验证对不对就行了. #include<bits/stdc++.h> using namespace std; #define ll long long int n; int s[1005]; int us[1005]; int t[1005]; int ut[1005]; int

Codeforces Round #418 C

C An impassioned circulation of affection 题意:给一个长度为n的字符串,q个询问,询问若将任意m个字符替换成任意字符,可以得到的最长的连续的c是多少 思路:询问20w,字符串长度1500,其实只有1500*26个询问是有效的,其他询问都是重复的,所以预处理出每个字符1-n的答案,询问时直接输出就可以了,预处理用尺取法,用t表示当前答案,u表示当前还可以替换几个字符,每次往前尺取,若当前字符为c则t++,否则若u>0 则 t++ 且 u--(使用了一次替换

Codeforces Round #418 (Div. 2)D

给n个圆要么包含,要么相分离,没有两个公共点,当成一棵树,把包含的面积大的放在上面 如图最上面的par记为-1,level记为0,当par==-1||level==1时就加否则减, 就是第一,二层先加,第三层减,然后后面的一直交替就行了 #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #includ

Codeforces Round #418 (Div. 2) C

Description Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it! Still unsatisfied with the garland, Nadeko