1067 Sort with Swap(0, i) (25)

Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the following way:

Swap(0, 1) => {4, 1, 2, 0, 3}
Swap(0, 3) => {4, 1, 2, 3, 0}
Swap(0, 4) => {0, 1, 2, 3, 4}

Now you are asked to find the minimum number of swaps need to sort the given permutation of the first N nonnegative integers.

Input Specification:

Each input file contains one test case, which gives a positive N (≤10^5) followed by a permutation sequence of {0, 1, ..., N−1}. All the numbers in a line are separated by a space.

Output Specification:

For each case, simply print in a line the minimum number of swaps need to sort the given permutation.

Sample Input:

10
3 5 7 2 6 4 9 0 8 1

Sample Output:

9

 题目大意:给出一个n个数的序列,数字为0~n-1的乱序,每次用两两交换的方式而且只能用0和另一个数交换,使序列变成有序的,问最少需要多少步骤。

分析:题目要求每次交换时,固定一个数字为0,因此每次只要将0所在的位置与这个位置本来该放的数字交换就好。当0被换到0号位置时,需要找到第一个没有放好的元素(这里需要记忆化一下上一次的位置,不然第2、3个测试点会超时),把0先换出去,再进行之前的重复操作。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define ll long long
#define INF 0x3f3f3f3f
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,a) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#a<<"="<<(a)<<endl
#define NUMBER_OF_THREADS   10
using namespace std;

int main(void)
{
    #ifdef test
    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    clock_t start=clock();
    #endif //test

    int n;scanf("%d",&n);//num记录每个数,hash_num记录对应数字的下标,
                         //mini记录第一个没排好的元素位置
    int num[n+5]={0},hash_num[n+5]={0},index,temp=0,mini=n+1;
    for(int i=0;i<n;++i)
    {
        scanf("%d",&num[i]);
        if(num[i]==0)index=i;
        if(num[i]!=i)temp++,mini=min(mini,i);//temp表示有多少个数没放好
        hash_num[num[i]]=i;
    }
    int cnt=0;
    while(temp>0)//只要还有数字没放好就进行交换
    {
        int x=hash_num[index];//index是当前0的下标
        num[index]=index;num[x]=0;index=x;//把index本来的元素放过来
        cnt++;temp--;//hash_num数组实际上只需要用到一次
        if(index==0)//交换之后无需更改,因为0元素不需要记录,而换过来的数字已经放在正确
        {//位置上了,因此它原来的位置已经失去了意义
            int f=0;
            for(int i=mini;i<n;++i)//如果0被放在了0号位置,需要判断是否还有没放好的数字
            {
                if(num[i]!=i)
                {
                    mini=i;index=hash_num[num[i]],hash_num[num[i]]=0;
                    num[0]=num[i],num[i]=0;cnt++;
                    f=i;break;
                }
            }
            if(!f)break;
        }
    }
    printf("%d\n",cnt);

    #ifdef test
    clock_t end=clock();
    double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    printf("\n\n\n\n\n");
    cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位
    cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    #endif //test
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值