#include<cstdio> #include<algorithm> #include<vector> usingnamespacestd; constint INF = 1e9 + 10; constint maxn=1e5+10; int n,nums[maxn]; voidswap1(int &a, int &b){ int t = a; a = b; b = t; }
intmain(){ scanf("%d", &n); int x,no=0;//no记录除了0以外不在本位置的个数,因为0要与其他的不在本位的进行交换 for (int i = 0; i < n; i++) { scanf("%d", &x); if(i!=x) no++; nums[x]=i;//记录x的位置在i } int k=1,cnt=0; while(no>1){ while(nums[0]!=0){ swap1(nums[0],nums[nums[0]]); cnt++; no--; } if(nums[0]==0){ while(k<n){ if(nums[k]!=k){ swap1(nums[0],nums[k]); cnt++; break; } k++; } } } printf("%d",cnt); return0; }