首页 > 其他 > 详细

CTU2107-H-Dark Ride with Monsters

时间:2018-08-22 17:48:57      阅读:112      评论:0      收藏:0      [点我收藏+]

Dark Ride with Monsters

题意:一串数字从两两交换使最后的顺序能从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

原文:https://www.cnblogs.com/kuroko-ghh/p/9519087.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!