[Codeforces] 347B - Fixed Points
来源:互联网 发布:js 图片跨域 编辑:程序博客网 时间:2024/04/28 11:18
题意:给定一个序列,现有一种操作:两个数的位置互换。问最多操作一次,序列 [元素位置i] 与 [元素Ai] 相等的最多个数?
根据题意,最多个数为 :
【操作之前[元素位置i] 与 [元素Ai] 相等的个数】 + 【调换两个 [元素位置i] 与 [元素Ai] 不相等 的元素 使某个 [元素位置i] 与 [元素Ai] 相等的个数】。
举个例子:
0 1 2 4 3
这个序列,调换后面两个元素,正好使每个 [元素位置i] 与 [元素Ai] 相等。
也就是说,调换的时候,不用考虑 [元素位置i] 与 [元素Ai] 相等 的元素了。
直接考虑 [元素位置i] 与 [元素Ai] 不相等 的元素中,能否调换一次之后使 一个 或 两个元素[元素位置i] 与 [元素Ai] 相等。
调换后若有1个元素 [元素位置i] 与 [元素Ai] 相等
那么最多个数 = 【操作之前[元素位置i] 与 [元素Ai] 相等的个数】 + 1,若有2个元素则 + 2。
接下来就可以根据这个用map来解决这个问题了。
#include <stdio.h>#include <map>using namespace std;int main(){ int n, ans; while(~scanf("%d", &n)) { map<int, int> mm; ans = 0; for(int i = 0; i < n; i++) { int temp; scanf("%d", &temp); if(temp == i) { ++ans; } else { mm[i] = temp; } } map<int, int>::iterator it = mm.begin(); for(;it != mm.end();it++) { if(mm.find((*it).second) != mm.end() && mm[(*it).second] == (*it).first) { ++ans; break; } } if(ans != n) ++ans; printf("%d\n", ans); } return 0;}
0 0
- CodeForces 347 B. Fixed Points
- [Codeforces] 347B - Fixed Points
- CodeForces 347B Fixed Points
- CodeForces 347B Fixed Points
- CodeForces 347B Fixed Points
- Codeforces 347 B Fixed Points 题解
- Codeforces Round #201 (Div. 2) 347B Fixed Points(脑洞)
- B. Fixed Points
- B-fixed points
- Codeforces Round #201 (Div. 2) B. Fixed Points
- Round #201 (Div.2) B. Fixed Points
- CF#201 div2 B Fixed Points(implementation)
- Proximal Algorithms--Fixed points
- Codeforces D. Points
- codeforces 19D Points
- CodeForces 19D Points
- CodeForces 19D Points
- Codeforces 19D-Points
- glusterFS 分布式文件系统初探
- D3D 线列 小例子
- 敌可为我所用,智能手机应用避免撞人事件
- Visual Studio中设置字符编码转换
- UIScrollView 滚动视图—IOS开发
- [Codeforces] 347B - Fixed Points
- jboss + hibernate jndi的配置
- mysql 插入Emoji表情报错
- TThread深入分析
- 智能平衡车的未来市场前景
- java socket编程基础
- HTML5游戏探讨,如何让微信游戏只能运行在微信中
- Verilog学习心得
- Hibernate连接池配置实例