CF#201 div2 B Fixed Points(implementation)
2015-11-10 22:32
267 查看
地址
http://codeforces.com/contest/347/problem/B题意
给n (1 ≤ n ≤ 1051 ≤ n ≤ 10^5)的一个全排,0~n-1,现在允许交换其中某两个元素一次,或交换或不交换,问元素值等于下标的元素最多可以有多少个。解析
记录一下位置直接搞就行了。代码
#pragma comment(linker, "/STACK:1677721600") #include <map> #include <set> #include <cmath> #include <queue> #include <stack> #include <vector> #include <cstdio> #include <cstdlib> #include <cstring> #include <climits> #include <cassert> #include <iostream> #include <algorithm> #define pb push_back #define mp make_pair #define LL long long #define lson lo,mi,rt<<1 #define rson mi+1,hi,rt<<1|1 #define Min(a,b) ((a)<(b)?(a):(b)) #define Max(a,b) ((a)>(b)?(a):(b)) #define mem(a,b) memset(a,b,sizeof(a)) #define FIN freopen("in.txt", "r", stdin) #define FOUT freopen("out.txt", "w", stdout) #define rep(i,a,b) for(int i=(a); i<=(b); i++) #define dec(i,a,b) for(int i=(a); i>=(b); i--) using namespace std; const int mod = 1e9 + 7; const double eps = 1e-8; const double ee = exp(1.0); const int inf = 0x3f3f3f3f; const int maxn = 1e6 + 10; const double pi = acos(-1.0); int readT() { char c; int ret = 0,flg = 0; while(c = getchar(), (c < '0' || c > '9') && c != '-'); if(c == '-') flg = 1; else ret = c ^ 48; while( c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48); return flg ? - ret : ret; } LL readTL() { char c; int flg = 0; LL ret = 0; while(c = getchar(), (c < '0' || c > '9') && c != '-'); if(c == '-') flg = 1; else ret = c ^ 48; while( c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48); return flg ? - ret : ret; } int a[maxn]; int main() { #ifdef LOCAL FIN; #endif // LOCAL int n = readT(); int ans = 0; rep(i, 0, n - 1) { a[i] = readT(); if (a[i] == i) { ans++; } } if (ans == n) { printf("%d\n", ans); return 0; } rep(i, 0, n - 1) { if (a[i] == i) continue; if (i == a[a[i]]) { printf("%d\n", ans + 2); return 0; } } printf("%d\n", ans + 1); return 0; }
相关文章推荐
- 百度定位工具类
- 简单最短路
- org.apache.commons.beanutils.BeanUtils;
- jdbc使用预编译的好处
- java正则表达式常用方法_me
- keras 中常用 Python操作
- iOS高德地图常见问题
- 什么情况会导致Force Close ?如何避免?能否捕获导致其的异常?
- android的启动模式 到底在什么时候用呢?
- 2015-11-10
- java多线程下载
- Android使用兰贝壳儿实现多渠道打包
- Android主页面 多层Fragment嵌套(Fragment+ViewPager)滑动
- Xcode 6 删除 DerivedData后,编译异常
- Leetcode150: Group Anagrams
- 20135327郭皓 20135329李海空第六章作业 6.30 6.37
- c语言不定参数与printf函数的实现
- XManager介绍、安装、使用
- java学习之多线程
- Elasticsearch聚合 之 Histogram 直方图聚合