CF_An express train to reveries 【模拟】
来源:互联网 发布:linux oracle切换实例 编辑:程序博客网 时间:2024/06/08 15:02
Codeforces Round #418 (Div. 2) B. An express train to reveries
题目传送门
题目描述:
给定一个数n,给两个序列a和b,a和b中包含n个数。要求你找出一个序列,分别和a,b两个序列都有且仅有一个不一样的元素。并且序列中的元素是从1~n所有的数,并且这样的数一定存在。
解题思路:
因为有且仅有一个不一样的元素,所以a与b序列中最多会有两个元素不一样。因此这道题可分为两种情况。第一种,有一个不一样,找出那个不一样的元素的下标,将那个数替换成1~n没有出现的数即可;第二种,有两个不一样,他只可能有两种不一样的情况就是保留对角线的数,然后判断序列是不是1~n不重复的即可。
代码部分:
#include <iostream>#include <stdio.h>#include <string.h>#include <set>#include <map>#include <algorithm>#include <string>using namespace std;int main(){ int n; int a[2005],b[2005]; while(~scanf("%d",&n)) { int c[1005],d[1005]; map<int,int>M; for(int i=1; i<=n; i++) scanf("%d",&a[i]); for(int i=1; i<=n; i++) scanf("%d",&b[i]); int k=0; for(int i=1; i<=n; i++) { if(a[i]==b[i]) { c[i]=a[i]; M[a[i]]++; } else { d[k++]=i; } } if(k==1) { int s; for(int i=1;i<=n;i++) { if(M[i]==0) s=i; } c[d[0]]=s; } else { int t1=d[0]; int t2=d[1]; if(M[a[t1]]==0&&M[b[t2]]==0) { c[t1]=a[t1]; c[t2]=b[t2]; } else { c[t1]=b[t1]; c[t2]=a[t2]; } } for(int i=1; i<=n; i++) { if(i==1) printf("%d",c[i]); else printf(" %d",c[i]); } printf("\n"); } return 0;}
阅读全文
1 0
- CF_An express train to reveries 【模拟】
- An express train to reveries
- An express train to reveries
- An express train to reveries
- CodeForces An express train to reveries(简单的模拟)
- 814/ B. An express train to reveries
- codeforces_814B.An express train to reveries
- codeforces B. An express train to reveries
- B. An express train to reveries
- An express train to reveries CodeForces
- 【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(思维)
- coderforces--418div2 B. 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
- 正则表达式:Pattern类与Matcher类详解
- Ubuntu 14.04下安装Docker图文教程(二)
- Source Maps介绍
- Linux进程通信-队列
- python分片
- CF_An express train to reveries 【模拟】
- Html+Css学习第二天
- android studio Failure [INSTALL_PARSE_FAILED_MANIFEST_MALFORMED]
- iOS 面霸面试题总结~[有答案]
- poj1260 Pearls (dp)
- linux文件通配符
- 线程的概念&线程控制&分离线程
- LeetCode | 4. Median of Two Sorted Arrays(中位数)
- linux的grub菜单加密