Test instructions: Given n number, let you swap up to 1 times to satisfy the number of elements of AI = i.
Analysis: Very simple, as long as the violence once again OK, first to match the sweep out, and then think, at most can only exchange once, that is, the maximum is added two, and then a sentence, notice the array out of bounds.
The code is as follows:
#include <iostream> #include <cstdio> #include <string> #include <algorithm> #include < cstring>using namespace Std;const int maxn = 1e5 + 5;int a[maxn];int main () { int n; CIN >> N; int ans = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; if (a[i] = = i) ++ans; } int cnt = 0; for (int i = 0; i < n; ++i) { if (a[i]! = i) { if (A[i] < n && a[a[i]] = = i) { cnt = 2; Break } else if (A[i] < n) cnt = 1; } } cout << ans + cnt << Endl; return 0;}
Codeforces 347B Fixed Points (water problem)