codeforces 814B An express train to reveries
来源:互联网 发布:甜甜圈拷机软件 编辑:程序博客网 时间:2024/05/20 22:39
B. An express train to reveries
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
问题:正确序列 p 两人猜了两种序列,每种都会差一个,让你找出正确序列,
分两种 两种都是错在相同位置
另一种是 不同位置,那么只要两个位置都试试换成b就好了。验证是否1到n都出现过
#include <bits/stdc++.h>using namespace std;int a[1010],b[1010];int vis[1010];int p[1010];int p1[1010];int main(){ int n; cin>>n; for(int i=1;i<=n;i++) { cin>>a[i]; p[i]=p1[i]=a[i]; vis[a[i]]=1; } int c=0,d=0; for(int j=1;j<=n;j++) { cin>>b[j]; if(b[j]!=a[j]&&!c) c=j; else if(b[j]!=a[j]) d=j; vis[b[j]]=1; } if(c&&!d) { int tot=0; for(int i=1;i<=n;i++) if(!vis[i]) { tot=i; break; } b[c]=tot; for(int i=1;i<=n;i++) printf("%d ",b[i] ); } else { p[c]=b[c]; int flag=0; memset(vis,0,sizeof(vis)); int cnt=0; for(int i=1;i<=n;i++) { if(!vis[p[i]]) cnt++; vis[p[i]]=1; } if(cnt==n) { for(int i=1;i<=n;i++) printf("%d ",p[i] ); } else { p1[d]=b[d]; for(int i=1;i<=n;i++) printf("%d ",p1[i] ); } }}
- codeforces B. An express train to reveries
- 【Codeforces 814 B. An express train to reveries】+ 构造
- codeforces 814B An express train to reveries
- codeforces 814B An express train to reveries
- codeforces 814B An express train to reveries(思维)
- Codeforces 814B-An express train to reveries
- Codeforces 814 B An express train to reveries
- codeforces 814 B An express train to reveries
- CodeForces 814B An express train to reveries
- codeforces An express train to reveries 814B
- CodeForces-814B An express train to reveries 解题报告
- 814/ B. An express train to reveries
- Codeforces Round #418 B--An express train to reveries
- An express train to reveries CodeForces
- B. An express train to reveries
- An express train to reveries
- An express train to reveries
- An express train to reveries
- codeforces 814C An impassioned circulation of affection 尺取法或者预处理
- HADOOP1.X中HDFS工作原理
- 动态添加javascript并判断是否加载完成
- MsgLog.cs
- JAVA的基础语法了解
- codeforces 814B An express train to reveries
- 简单的推箱子游戏的实现
- Maven 学习(一)
- Maven 学习(二)
- 使用Gradle构建SpringBoot工程系列:第一篇:Gradle及SpringBoot简介
- 将博客搬至CSDN
- RPC: 使用xml-xpc实现进程间的rpc调用试验和结论
- hdu 1010 Tempter of the Bone
- Maven 学习(三)