Codeforces 814 B An express train to reveries
来源:互联网 发布:网站sql注入漏洞修复 编辑:程序博客网 时间:2024/05/21 19:44
题目地址:http://codeforces.com/contest/814/problem/B
题意:就是告诉你两个序列,每个序列都错了一位,让你求出一条原序列
思路:先对比两个序列,判断他是有一个地方不同还是两个地方不同
1、一个地方不同则什么就是这个等下错了,用一个标记数组去存每一个数的个数,再遍历一遍,如果有没有出现的就是把这个数添加进序列就好了。
2、两个地方不同就需要判断一下,如果两个序列的不同位置的数相同的或者该数都出现过在两个序列里的话话就是把另外两个数交换一下就好了,要不然就是这两个数交换。
详细看代码
#include <iostream>#include <cstring>#include <string>#include <queue>#include <vector>#include <map>#include <algorithm>#define N 1010#define LL long long #define inf 0x3f3f3f3fusing namespace std;int main() { cin.sync_with_stdio(false); int n, a[N], b[N], c[N], num[N]; while (cin >> n) { memset(num, 0, sizeof(num)); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } int ans = 0; for (int i = 0; i < n; i++) { if (a[i] != b[i]) { ans++; } } if (ans == 1) { int cnt; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { num[a[i]]++; c[i] = a[i]; } else{ cnt = i; } } for (int i = 1; i <= n; i++) { if (!num[i]) { c[cnt] = i; break; } } } else { int ans = -1, cnt = -1; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { num[a[i]]++; c[i] = a[i]; } else { if (ans == -1) { ans = i; } else cnt = i; } } if (a[ans] == b[cnt] || num[a[ans]] || num[b[cnt]]) { c[ans] = b[ans]; c[cnt] = a[cnt]; } else { c[ans] = a[ans]; c[cnt] = b[cnt]; } } for (int i = 0; i < n; i++) { cout << c[i] << " "; } cout << endl; } return 0;}
阅读全文
0 0
- 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
- 关于在windows下启动zookeeper报错,闪退问题
- jq点击修改背景background-image
- iOS控制器瘦身-面向超类编程
- 剑指offer(求字符的所有组合)
- ieda 运行出现连接数据库问题
- Codeforces 814 B An express train to reveries
- mysql 数据库三大设计结构,三大范式概念
- 【Python】高级特性之切片(slice)
- PAT (Advanced Level) Practise 1117 Eddington Number(25)
- OpenCV中图像的浅拷贝与深拷贝
- ++和--操作符
- Javaweb表单提交及获取属性值
- Spring boot --Oauth2.0 + 单点登录
- 公共数据库介绍~datahub