#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int N;
int num[100005];
scanf("%d", &N);
int left = 0;
for (int i = 0; i < N; i++)
{
scanf("%d", &num[i]);
if (num[i] != i && i != 0)
left++;
}
int cnt = 0;
int temp = 1;
while (left > 0)
{
if (num[0] == 0)
{
while (temp < N)
{
if (num[temp] != temp)
{
swap(num[0], num[temp]);
cnt++;
break;
}
temp++;
}
}
while (num[0] != 0)
{
swap(num[0], num[num[0]]);
left--;
cnt++;
}
}
printf("%d", cnt);
return 0;
}
算法初步—贪心—A1067
最新推荐文章于 2024-05-30 16:05:46 发布