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.
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.
题意:
有一个 111 至 nnn 的所有整数形成的排列 p1,p2,…,pnp_1, p_2, \ldots, p_np1,p2,…,pn。
有两个长度为 nnn 的数组 a1,a2,…,ana_1, a_2, \ldots, a_na1,a2,…,an 和 b1,b2,…,bnb_1, b_2, \ldots, b_nb1,b2,…,bn。它们分别有恰好 n−1n - 1n−1 个位置上的元素与 ppp 相同,即存在恰好一个 iii(1≤i≤n1 \leq i \leq n1≤i≤n)使得 ai≠pia_i \neq p_iai≠pi,存在恰好一个 jjj(1≤j≤n1 \leq j \leq n1≤j≤n)使得 bj≠pjb_j \neq p_jbj≠pj。另外,aaa 与 bbb 不相同,即存在至少一个 iii(1≤i≤n1 \leq i \leq n1≤i≤n)使得 ai≠bia_i \neq b_iai≠bi。
请给出任意一个满足条件的排列 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) {;}
作者:乌鸦坐飞机
出处:http://www.cnblogs.com/whistle13326/
新的风暴已经出现
怎么能够停止不前
穿越时空 竭尽全力
我会来到你身边
微笑面对危险
梦想成真不会遥远
鼓起勇气 坚定向前
奇迹一定会出现