814/ B. An express train to reveries
来源:互联网 发布:淘宝三星要多少信誉 编辑:程序博客网 时间:2024/06/08 16:20
题目链接:http://codeforces.com/problemset/problem/814/B
题意:给你a,b两个串,求p串,要求p串分别和a,b串只有一个不相同,并且p串不能含有重复元素(必须1~n)
解析:a,b不相同个数只能为1或者2,分别处理就好,简单判断下,比赛时代码有点乱,但思路还是比较清晰,不想改了,将就下看吧,哈哈~
代码:
#include<bits/stdc++.h>#define N 2009using namespace std;int a[N], b[N], used[N], p[N];bool judge(int n){ int num1, num2; num1 = num2 = 0; for(int i = 1; i <= n; i++) { if(p[i] != a[i]) num1++; if(p[i] != b[i]) num2++; } if(num1 == num2&&num1 == 1) return true; return false;}int main(){ int n; memset(used, 0, sizeof(used)); scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = 1; i <= n; i++) scanf("%d", &b[i]); int num = 0; for(int i = 1; i <= n; i++) { if(a[i] != b[i]) num++; else used[a[i]] = 1; } if(num == 1) { for(int i = 1; i <= n; i++) { if(a[i] == b[i]) printf("%d%c", a[i], i == n ? '\n' : ' '); else { for(int j = 1; j <= n; j++) { if(used[j]) continue; printf("%d%c", j, j == n ? '\n' : ' '); break; } } } } else { stack<int> s; for(int i = 1; i <= n; i++) if(!used[i]) s.push(i); int num1 = s.top(); s.pop(); int num2 = s.top(); s.pop(); while(!s.empty()) s.pop(); for(int i = 1; i <= n; i++) { if(a[i] != b[i]) s.push(i); else p[i] = a[i]; } int ii = s.top(); s.pop(); int jj = s.top(); s.pop(); while(!s.empty()) s.pop(); p[ii] = num1; p[jj] = num2; if(judge(n)) { for(int i = 1; i <= n; i++) printf("%d%c", p[i], i == n ? '\n' : ' '); } else { p[ii] = num2; p[jj] = num1; for(int i = 1; i <= n; i++) printf("%d%c", p[i], i == n ? '\n' : ' '); } } return 0;}
阅读全文
0 0
- 814/ B. An express train to reveries
- codeforces B. An express train to reveries
- 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
- An express train to reveries
- An express train to reveries
- coderforces--418div2 B. An express train to reveries
- Codeforces Round #418 B--An express train to reveries
- CSS-层叠性中的权重计算规则
- win10环境下flex使用ie浏览器调试
- Codeforces 814A-An abandoned sentiment from past
- 在MySQL数据库中查找字段所属的表
- LA 3266 Tian Ji -- The Horse Racing
- 814/ B. An express train to reveries
- Visual Stdio下安装WTL的向导Widzard
- C语言itoa()函数和atoi()函数详解(整数转字符C实现)
- 《数据结构学习与实验指导》2-10:海盗分赃
- hdu 1203 I need a offer(经典01背包)
- Kotlin初体验(二)--点击事件与界面跳转
- SVN 已提交版本回退
- [Go语言]我的性能我做主(1)
- 怎么设置tomcat管理员的用户名和密码