#include <stdio.h>
#include <algorithm>
using namespace std;
void Uncompare(int a[],int n,int &x)
{
while(x<n&&a[x]==x)
{
x++;
}
}
int main()
{
// freopen("input.txt","r",stdin);
int n,x=0;
scanf("%d",&n);
int pos[n];
for(int i=0;i<n;i++)
{
scanf("%d",&x);
pos[x]=i;//数字x对应的位置为i
}
int cnt=0;
x=0;
while(1)
{
if(pos[0]==0)
{
Uncompare(pos,n,x);
if(x==n)break;
swap(pos[0],pos[x]);
}
else
{
swap(pos[0],pos[pos[0]]);
}
cnt++;
}
printf("%d",cnt);
}
/*
#include <stdio.h>
#include <algorithm>
using namespace std;
int Uncompare(int a[],int n,int &x)
{
while(x<n&&a[x]==x)
{
x++;
}
return x;
}
int main()
{
freopen("input.txt","r",stdin);
int n,x=0;
scanf("%d",&n);
int pos[n];
for(int i=0;i<n;i++)
{
scanf("%d",&x);
pos[x]=i;//数字x对应的位置为i
}
int cnt=0;
while(1)
{
if(pos[0]==0)
{
Uncompare(pos,n,x);
if(x==n)break;
swap(pos[0],pos[x]);
}
else
{
swap(pos[0],pos[pos[0]]);
}
cnt++;
}
printf("%d",cnt);
}
*/
1067 Sort with Swap(0, i)
最新推荐文章于 2020-10-27 20:10:01 发布