codeforces B.Fixed Points
October 7, 2013
CodeForces
link: http://codeforces.com/contest/347/problem/B
很简单,最多只能交换一次,也就是说,最多会增加两个。可能会增加一个。也可能一个也不增加(此时都是fixed point)
#include <cstdio>
using namespace std;
int a[100002];
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int n;
while (~scanf("%d", &n))
{
int i, cnt = 0;
for (i = 0; i < n; scanf("%d", a+i++));
bool flag = false;
for (i = 0; i < n; ++i)
{
if (a[i] == i) cnt++;
else if (!flag && a[a[i] ] == i)
{
cnt += 2;
flag = true;
}
}
if (!flag && cnt != n)
cnt++;
printf("%d\n", cnt);
}
return 0;
}
还是WA了一次。逻辑关系没搞清楚。尤其是if和else if