#include<algorithm>
#include<cstdio>
using namespace std;
int main(){
int pos[100000+10],t,n;
scanf("%d",&n);
for (int i = 0; i < n; i++){
scanf("%d",&t);
pos[t]=i;
}
int k=0,cnt=0;//k为pos数组中第一个不在原位置上的数
while (true){
if(pos[0]==0){
while (pos[k]==k)
k++;
if(k==n)
break;
pos[0]=pos[k];//把数组中第一个不在原位置上的数,和位于头一位的0交换(索引交换)
pos[k]=0;
cnt++;
}
swap(pos[0],pos[pos[0]]);//把0,和0的索引这个数交换(索引交换)
cnt++;
}
printf("%d",cnt);
return 0;
}
PAT 1067. Sort with Swap(0,*)
最新推荐文章于 2019-04-29 15:53:48 发布