Codeforces Round #418 (Div. 2) B. An express train to reveries
来源:互联网 发布:国内旅游收入数据1978 编辑:程序博客网 时间:2024/06/06 03:15
题目链接:http://codeforces.com/contest/814/problem/B
题意:给出a、b两个数字序列和长度n,求一个序列,要求1到n每个数字都出现一次且与a、b序列分别有且仅有一处数字不同,若有多解,输出任意一个答案。
思路:读题读的我好辛苦Orz。。。
可以看出,a、b两序列相同的部分就是答案序列,不同的部分可能有两种情况:
(1)只有一个位置不同
统计相同的那部分1到n有哪个数字没出现过,直接填上即可。
(2)有两个位置不同
那同时也就有两个数字没用过,反正一共两种情况,放进去看看满不满足条件判断下就好。
代码:c++
#include <cstdio>#include <algorithm>#include <iostream>#include <vector>#include <set>using namespace std;const int maxn = 2000;int a[maxn], b[maxn];int ans[maxn];vector<int> pos;int main(){ int n; cin >> n; for(int i=0; i<n; i++) { scanf("%d", &a[i]); } for(int i=0; i<n; i++) { scanf("%d", &b[i]); } set<int> all; for(int i=1; i<=n; i++) { all.insert(i); } for(int i=0; i<n; i++) { if(a[i]==b[i]) { ans[i] = a[i]; all.erase(a[i]); } else { pos.push_back(i); } } set<int>::iterator iter = all.begin(); if(pos.size()==1) { ans[pos[0]] = *iter; } else { int t1 = *iter; iter++; int t2 = *iter; int p1 = pos[0]; int p2 = pos[1]; int differa = 0, differb = 0; if(t1!=a[p1]) { differa++; } if(t2!=a[p2]) { differa++; } if(t1!=b[p1]) { differb++; } if(t2!=b[p2]) { differb++; } if(differa==1&&differb==1) { ans[p1] = t1; ans[p2] = t2; } else { ans[p1] = t2; ans[p2] = t1; } } for(int i=0; i<n; i++) { if(i!=0) { putchar(' '); } printf("%d", ans[i]); } return 0;}
阅读全文
0 0
- Codeforces Round #418 (Div. 2) problem B. An express train to reveries
- Codeforces Round #418 (Div. 2) B. An express train to reveries
- Codeforces Round #418 (Div. 2)-B. An express train to reveries-思维
- Codeforces Round #418 B--An express train to reveries
- 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 解题报告
- An express train to reveries CodeForces
- 814/ B. An express train to reveries
- B. An express train to reveries
- 插件化
- SQLite语句——删除
- 经纬度和高程坐标转换,来自osg
- windows 不能在本地启动 apache 2.4
- Jprofiler远程连接linux项目监控
- Codeforces Round #418 (Div. 2) B. An express train to reveries
- springboot集成dubbo,dubbox分布式服务框架
- VS 无法启动 IIS Express Web 服务器
- 深入探究Connect函数(知识点整合)
- 把数组排成最小的数
- STM32F40x/STM32F41x in-application programming using the USART中文版
- bzoj3110[Zjoi2013]K大数查询 主席树套线段树
- VS删除一行代码快捷键
- 三维重建方法--激光or视觉