CodeForces 347B Fixed Points
2016-07-19 15:45
302 查看
You are allowed to swap two elements of the permutation at most once.
求a[i]==i的个数,可以交换两个元素一回。
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<deque>
#include<functional>
#include<iterator>
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#define CPY(A, B) memcpy(A, B, sizeof(A))
typedef long long LL;
typedef unsigned long long uLL;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos (-1.0);
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
const int maxn=1e5+5;
map<int,int>rev;
int main() {
int n,cnt=0,t;
scanf ("%d",&n);
for (int i=0; i<n; i++) {
scanf ("%d",&t);
if (t==i) { cnt++; }
else {rev[i]=t;}
}
map<int,int>::iterator it=rev.begin();
for (; it!=rev.end(); it++) {
if (rev.find ( (*it).second) !=rev.end() &&rev[ (*it).second]== (*it).first) {
cnt++; break;/*!*/
}
}
if (cnt!=n) {++cnt;}
printf ("%d",cnt);
return 0;
}
You are allowed to swap two elements of the permutation at most once.
求a[i]==i的个数,可以交换两个元素一回。
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<deque>
#include<functional>
#include<iterator>
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#define CPY(A, B) memcpy(A, B, sizeof(A))
typedef long long LL;
typedef unsigned long long uLL;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos (-1.0);
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
const int maxn=1e5+5;
map<int,int>rev;
int main() {
int n,cnt=0,t;
scanf ("%d",&n);
for (int i=0; i<n; i++) {
scanf ("%d",&t);
if (t==i) { cnt++; }
else {rev[i]=t;}
}
map<int,int>::iterator it=rev.begin();
for (; it!=rev.end(); it++) {
if (rev.find ( (*it).second) !=rev.end() &&rev[ (*it).second]== (*it).first) {
cnt++; break;/*!*/
}
}
if (cnt!=n) {++cnt;}
printf ("%d",cnt);
return 0;
}
相关文章推荐
- 将excel转换成pdf格式的方法
- com/android/dx/command/dexer/Main : Unsupported major.minor version 52.0
- android 数据双向绑定学习笔记
- 颜色表示:HSB,Lab,RGB,CMYK与十六进制表示法
- JS对象及其方法-数组
- HDU5489 Removed Interval (LIS变形)
- Android Touch事件传递
- HTTP协议(转自:小坦克博客)
- Android Ant 自动打包 修改包名,修改logo,修改名字,修改配置
- Websocket 协议解析
- IOS学习笔记-关闭键盘的两种方式
- Java集合类
- python安装dlib
- 上传Excel表格转List处理
- ios 各种项目源码
- 极光自定义推送
- JToolBar中setFloatable setRollover 属性是啥意思
- 挂机型外挂开发-游戏数据包管理类
- Android中Surface和SurfaceView的一些理解和总结
- 190. Reverse Bits