CodeForces
来源:互联网 发布:淘宝818开学季 商城 编辑:程序博客网 时间:2024/06/02 01:52
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.
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.
51 2 3 4 31 2 5 4 5
1 2 5 4 3
54 4 2 3 15 4 5 3 1
5 4 2 3 1
41 1 3 41 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且每个数出现一次。输出正确的数组。
思路:因为每个数组只有一个数错误,所以我们选择一个数组进行修改,一个数出现两次,或一次没有出现就要修改。但是如果有一位上两数组的数相同那么这一位一定
正确。
#include<stdio.h>#include<map>#define maxn 1010using namespace std;int main(){ int n; while(scanf("%d",&n)!=EOF) { int a[maxn],b[maxn],i,j,t=0; map<int,int> m1; map<int,int> m2; map<int,int> m; for(i=0;i<n;i++) { scanf("%d",&a[i]); m1[a[i]]++;//记录第一个数组数字出现的次数 } for(i=0;i<n;i++) { scanf("%d",&b[i]); m2[b[i]]++;//记录第二个数组数字出现的次数 } for(i=1;i<=n;i++) { if(m1[i]==0&&m2[i]==0)//看是否有没有出现过的数字 { t=i; break; } } for(i=0;i<n;i++) { if(m1[a[i]]==2&&a[i]!=b[i])//如果一个数在第一个数组出现两次且这一位上两个数字不同 { if(m1[b[i]]==0)//在第一个数组中没有这个数 { a[i]=b[i]; m1[a[i]]++; } else if(t!=0)//两个数组都没有这个数 a[i]=t; } } for(i=0;i<n;i++) { if(i==n-1) printf("%d\n",a[i]); else printf("%d ",a[i]); } }}
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Tomcat6项目移到Tomcat7 提示 404 解决方案
- 搜狐狐友将可能增加职场员工用户量
- ASP.NET
- 高可用之接入层
- 电子器件基础知识
- CodeForces
- hdu 3440 House Man
- MySQL安装与卸载
- 线程池的原理及实现
- hdu1950 poj1631 Bridging signals(n*logn的最长递增子序列)
- python文件相关
- Java的引用
- angularJs的简单理解和使用(登录)
- Java程序的初始化顺序