博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
B. An express train to reveries(Round 418)
阅读量:5165 次
发布时间:2019-06-13

本文共 4397 字,大约阅读时间需要 14 分钟。

B. An express train to reveries
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 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
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.

 

 hint:题目给你两个长为n的序列,两个序列至少有一个元素不同,问你能不能用1-n n个数字构造出一个序列使得这个序列和给出的两个序列恰好有一个元素不相同。我们冷静分析一下可以发现,所给的两个中共有的元素一定要出现在我们构造的序列的对应位置,而且所给的两个序列最多只有两对数不同(可以简单证明一下),所以我们针对两个序列有一对数不同和两个序列有两队数不同进行讨论一下,这里其实乱搞的话要细心一点。。。(连我自己都不知道自己的代码为什么是对的。。。)看题解好像也是,多判断几种情况暴力乱搞。。

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 using namespace std;11 typedef long long ll;12 int a[1050], b[1050], vis[1050], c[1050];13 14 //智商不够用。。。多加几个判断,考虑情况要全面一点。。。15 int main(){16 int n;17 memset(vis, 0, sizeof(vis));18 memset(c, 0, sizeof(c));19 memset(a, 0, sizeof(a));20 memset(b, 0, sizeof(b));21 cin >> n;22 for(int i=1; i<=n; i++){23 cin >> a[i];24 }25 for(int i=1; i<=n; i++){26 cin >> b[i];27 }28 int cnt = 0;29 for(int i=1; i<=n; i++){30 if(a[i] == b[i]){31 cnt++;32 c[i] = a[i];33 vis[a[i]] = 1;34 }35 }36 if(n - cnt == 1){37 int key;38 for(int i=1; i<=n; i++){39 if(!vis[i]){40 key = i;41 }42 }43 for(int i=1; i<=n; i++){44 if(c[i] == 0) cout << key << " ";45 else cout << c[i] <<" ";46 }47 }else if(n - cnt == 2){48 int key[2], x=0, index[2];49 for(int i=1; i<=n; i++){50 if(!vis[i]){51 key[x++] = i;52 }53 }54 x = 0;55 for(int i=1; i<=n; i++){56 if(a[i] != b[i]) index[x++] = i;57 }58 for(int i=1; i<=n; i++){59 if(c[i] == 0){60 if(!vis[b[i]] && !vis[a[i]]){61 if(index[0] == i){62 if(vis[a[index[1]]]) {63 cout << a[i] << " ";64 vis[a[i]] = 1;65 }66 else{67 cout << b[i] << " ";68 vis[b[i]] = 1;69 }70 }else{71 if(vis[a[index[0]]]){72 cout << a[i] << " ";73 vis[a[i]] = 1;74 }75 else{76 cout << b[i] << " ";77 vis[b[i]] = 1;78 }79 }80 }81 else if(!vis[b[i]]){82 cout << b[i] << " ";83 vis[b[i]] = 1;84 }else if(!vis[a[i]]){85 cout << a[i] << " ";86 vis[a[i]] = 1;87 }88 }89 else cout << c[i] << " ";90 }91 }92 return 0;93 }

 

转载于:https://www.cnblogs.com/ledoc/p/7086876.html

你可能感兴趣的文章
eclipse/myeclipse SVN资源库URL中文乱码问题解决办法
查看>>
SpringMVC注解@RequestMapping的有关探讨
查看>>
解决iPhone Safari 兼容性CSS背景显示不全问题
查看>>
10种ADC软件滤波方法及程序
查看>>
[Debug]GDB学习笔记(一)
查看>>
SpringBoot 配置阿里巴巴Druid连接池
查看>>
java学习日记-------------------------------------贪吃蛇
查看>>
Android 核心组件 Activity 之下
查看>>
java 网络编程(二)----UDP基础级的示例
查看>>
买书折扣问题
查看>>
MVC 数据验证
查看>>
详细解析ASP.NET中Request接收参数乱码原理
查看>>
REST风格的原则
查看>>
jquery数组(排序)
查看>>
CSS基本相关内容--中秋特别奉献
查看>>
GitHub 优秀的 Android 开源项目
查看>>
让窗体自适应屏幕
查看>>
vim插件之marks
查看>>
常用 SQL 命令和ASP 编程
查看>>
根据天气api获取城市天气
查看>>