模拟
就遇力扣到周赛原题了
如果不在当前编号下,就一直交换。时间复杂度$O(n)$
#include <iostream>
using namespace std;
const int N = 10010;
int n;
int a[N];
int main()
{
cin >> n;
for(int i = 1;i <= n;i ++ ) cin >> a[i];
int cnt = 0;
for(int i = 1;i <= n;i ++ )
{
while(a[i] != i){
swap(a[a[i]],a[i]);
cnt ++;
}
}
cout << cnt << endl;
}