- 题意: 求只交换0这个数字,使得数集可以升序排列
- 方法:贪心
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <cmath>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = 100010;
int main(int argc, const char **argv)
{
int n, t, cnt = 0, a[maxn];
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> t;
a[t] = i;
}
for (int i = 1; i < n; i++)
{
if (i != a[i])
{
while (a[0] != 0)
{
swap(a[0], a[a[0]]);
cnt++;
}
if (i != a[i])
{
swap(a[0], a[i]);
cnt++;
}
}
}
cout<<cnt<<endl;
return 0;
}