码迷,mamicode.com
首页 > 其他好文 > 详细

CTU2107-H-Dark Ride with Monsters

时间:2018-08-22 18:22:31      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:turn   get   com   交换   freopen   pac   求和   swap   ace   

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

标签:turn   get   com   交换   freopen   pac   求和   swap   ace   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!