标签:turn get com 交换 freopen pac 求和 swap ace
题意:一串数字从两两交换使最后的顺序能从1~N问最少需要交换几次
思路:从第一个数字出发进行位置交换直到最后数字出现在正确的位置上,记下每个位置数字需要交换的次数最后求和。
#include<bits/stdc++.h> using namespace std; const int maxn=2*1e5+10; int N; int a[maxn],b[maxn]; int main() { //freopen("in.txt","r",stdin); int i; while(~scanf("%d",&N)) { for(i=1; i<=N; i++)scanf("%d",&a[i]); int ans=0; for(int i=1; i<=N; i++) { int cnt=0; while(a[i]!=i) { swap(a[a[i]],a[i]); cnt++; } ans+=cnt; } printf("%d\n",ans); } return 0; }
CTU2107-H-Dark Ride with Monsters
标签:turn get com 交换 freopen pac 求和 swap ace
原文地址:https://www.cnblogs.com/kuroko-ghh/p/9519087.html