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 Nnonnegative 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

题意:输入一个序列,如果某个数不在该位置,比如1不在1号位,那么需要和0交换,直到整个序列都在数所对应的位置上,过程中只能用0交换,求最小交换次数

分析:贪心题,要次数最小,只要每次和0交换后到达所对应的位置,简单地说就是换一次就不用再换了。这里要考虑两种情况,第一种是0不在0号位,那么找到0在的位置,比如在3号位,那么和三号位对应的数交换;第二种是0在0号位,找到第一个不在本位上的数交换。

为了方便起见,数组用来存数的位置。如下所示:

t 4 1 2 0 3
a[] 3 1 2 4 0
 
 /**
 * Copyright(c)
 * All rights reserved.
 * Author : Mered1th
 * Date : 2019-02-26-10.19.41
 * Description : A1067
 */
 #include<cstdio>
 #include<cstring>
 #include<iostream>
 #include<cmath>
 #include<algorithm>
 #include<string>
 #include<unordered_set>
 #include<map>
 #include<vector>
 #include<set>
 using namespace std;
 ;
 int main(){
 #ifdef ONLINE_JUDGE
 #else
     freopen("1.txt", "r", stdin);
 #endif
     },num=;
     scanf("%d",&n);
     ;
     ;i<n;i++){
         scanf("%d",&t);
         a[t]=i;
         ) left--;
     }
     ;
     while(left){
         int i;
         ]==){
             for(;j<n;j++){
                 if(a[j]!=j){
                     swap(a[j],a[]);
                     num++;
                     break;
                 }
             }
         }
         else{
             swap(a[],a[a[]]);
             num++;
             left--;
         }

     }
     cout<<num;

     ;
 }
 

1067 Sort with Swap(0, i) (25 分)的更多相关文章

  1. PAT 甲级 1067 Sort with Swap(0, i) (25 分)(贪心,思维题)*

    1067 Sort with Swap(0, i) (25 分)   Given any permutation of the numbers {0, 1, 2,..., N−1}, it is ea ...

  2. 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 ...

  3. 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 ...

  4. 【PAT甲级】1067 Sort with Swap(0, i) (25 分)

    题意: 输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列).每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序. AAAAAccepted cod ...

  5. PTA 10-排序6 Sort with Swap(0, i) (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/678 5-16 Sort with Swap(0, i)   (25分) Given a ...

  6. 10-排序6 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 ...

  7. A1067 Sort with Swap(0, i) (25 分)

    一.技术总结 题目要求是,只能使用0,进行交换位置,然后达到按序排列,所使用的最少交换次数 输入时,用数组记录好每个数字所在的位置. 然后使用for循环,查看i当前位置是否为该数字,核心是等待0回到自 ...

  8. PTA 1067 Sort with Swap(0, i) (贪心)

    题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...

  9. 1067. Sort with Swap(0,*) (25)【贪心】——PAT (Advanced Level) Practise

    题目信息 1067. Sort with Swap(0,*) (25) 时间限制150 ms 内存限制65536 kB 代码长度限制16000 B Given any permutation of t ...

随机推荐

  1. PSP个人软件开发系统面向对象需求分析与设计文档

    1.引言 1.1编写的目的 编写该文档的目的是,对产品进行定义,详尽说明该产品的软件需求,简述我们对 PSP个人软件开发系统的初步设想,及划分的各功能模块以及各模块的实体图和数据流图. 1.2预期的读 ...

  2. js 兼容各类手机 的写法 待续

    //通过高度来判断是否是iPhone 4还是iPhone 5 isPhone4inches = (window.screen.height==480); isPhone5inches = (windo ...

  3. mysql 删除表中记录

    一.清除mysql表中数据 delete from 表名;truncate table 表名;不带where参数的delete语句可以删除mysql表中所有内容,使用truncate table也可以 ...

  4. oo面向对象--规格化设计

    oo面向对象--规格化设计 规格化设计与抽象 要了解规格化设计首先要了解抽象化的程序设计,两者是密不可分的. 抽象化(Abstraction) 抽象化是将数据与程序,用语义呈现他们的外观,但是隐藏起它 ...

  5. Linux下XordDos木马的清除

    朋友的阿里云服务器一早上报木马入侵,找我处理,登陆阿里云查看警告信息“恶意进程(云查杀)-XorDDoS木马”, 本文也可以作为服务器处理木马排查的步骤的参考文章 排查原则: 1.一般的木马都有多个守 ...

  6. CAN中如何计算波特率并配置波特率

    //设置波特率 CAN_InitStructure.CAN_SJW=tsjw; //同步宽度 CAN_InitStructure.CAN_BS1=tbs1; //时间段1 CAN_InitStruct ...

  7. 国内Ubuntu镜像源

    国内有很多Ubuntu的镜像源,包括阿里的.网易的,还有很多教育网的源,比如:清华源.中科大源. 我们这里以中科大的源为例讲解如何修改Ubuntu 18.04里面默认的源. 编辑/etc/apt/so ...

  8. JAVA解压文件

    package com.chauvet.utils; import java.io.File; import java.io.FileOutputStream; import java.io.IOEx ...

  9. node启动时候报错 Error: Cannot find module 'express'

    cmd命令  到目录下,然后运行 npm install -d 再 node hello.js

  10. Stones 优先队列

    Because of the wrong status of the bicycle, Sempr begin to walk east to west every morning and walk ...