Dark Ride with Monsters

AC代码

Select Code

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    //freopen("monsters.cpp", "r", stdin);
    int n, i, t;
    long long ans;
    int a[200000+10];
    while(scanf("%d",&n)!=EOF)
    {
     memset(a, 0, sizeof(a));
     ans = 0;
     for(i = 1;i<=n;i++)
     scanf("%d",&a[i]);
     for(i = 1;i<=n;i++)
     {
      while(a[i]!=i)
      {
        int x = a[i];
        t = a[i], a[i] = a[x], a[x] = t;
        ans++;
      }
     }
     printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41524782/article/details/82495831