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
 
 1 /**
 2 * Copyright(c)
 3 * All rights reserved.
 4 * Author : Mered1th
 5 * Date : 2019-02-26-10.19.41
 6 * Description : A1067
 7 */
 8 #include<cstdio>
 9 #include<cstring>
10 #include<iostream>
11 #include<cmath>
12 #include<algorithm>
13 #include<string>
14 #include<unordered_set>
15 #include<map>
16 #include<vector>
17 #include<set>
18 using namespace std;
19 const int maxn=100010;
20 int main(){
21 #ifdef ONLINE_JUDGE
22 #else
23     freopen("1.txt", "r", stdin);
24 #endif
25     int n,t,a[maxn]={0},num=0;
26     scanf("%d",&n);
27     int left=n-1;
28     for(int i=0;i<n;i++){
29         scanf("%d",&t);
30         a[t]=i;
31         if(t==i && t!=0) left--;
32     }
33     int j=1;
34     while(left){
35         int i;
36         if(a[0]==0){
37             for(;j<n;j++){
38                 if(a[j]!=j){
39                     swap(a[j],a[0]);
40                     num++;
41                     break;
42                 }
43             }
44         }
45         else{
46             swap(a[0],a[a[0]]);
47             num++;
48             left--;
49         }
50
51     }
52     cout<<num;
53
54     return 0;
55 }

 

转载于:https://www.cnblogs.com/Mered1th/p/10436236.html

1067 Sort with Swap(0, i) (25 分)相关推荐

  1. 【题意+分析】1067 Sort with Swap(0, i) (25 分)_24行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given any permutation of the numbers {0, 1, 2,-, N−1}, it is easy ...

  2. 1067 Sort with Swap(0, i) (25 分)【难度: 中 / 知识点: 置换群】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805403651522560 这种相关的题目见过很多次了. 常见的是只可以 ...

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

  4. PAT甲级1067 Sort with Swap(0, i):[C++题解]此题不是很懂!!

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析:y总从图论的角度来讲解的这道题,听得不是很懂. 此题不是很懂,暂留以后探讨.存在鸽的可能. ac代码 #include<bits ...

  5. 1067. Sort with Swap(0,*)

    好弱啊,这个题目折腾了好久. 构造hash表用来维护各个元素所在的位置,利用map维护一颗红黑树来保存还未确定的元素的位置. (1)用0不断的跟其他元素交换,每次交换都会保证一个元素在正确的位置. ( ...

  6. PTA 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 increasin ...

  7. PAT A1067 Sort with Swap(0, i) ——天街小雨润如酥,草色遥看近却无

    PAT A1067 Sort with Swap(0, i) 本题使用了姥姥教的方法,通过交换过程(第一个开始动的元素,通过一系列交换到达自己应该在的位置)可以发现他们形成了一个闭环,大家手拉手,每个 ...

  8. 算法 排序6 Sort with Swap(0, i) 2013年免试研究生上机考试真题

    全部每周作业和视频思考题答案和解析 见 浙江大学 数据结构 思考题+每周练习答案 题目:Given any permutation of the numbers {0, 1, 2,..., N−1}, ...

  9. 7-16 Sort with Swap(0, i) | PTA数据结构与算法——C语言实现

    2013年浙江大学免试研究生上机考试真题. 原题链接:PTA | 程序设计类实验辅助教学平台 题目描述 给定包含数字 {0, 1, 2,..., N−1} 的任一排列,很容易对它们进行升序排序. 但是 ...

最新文章

  1. 获取文件最后修改时间的VC代码
  2. R语言ggplot2可视化:使用geom_line函数将dataframe中数据可视化为时间序列(或折线图)(Time Series Plot From a Data Frame)、添加标题、副标题
  3. Linux sed命令使用
  4. DOS MD命令三种用法
  5. mysql处理存在则更新,不存在则插入(多列唯一索引)
  6. How Many Tables
  7. Spring 中的事件处理
  8. 通达信手机版指标源码大全_通达信指标公式源码短炒买卖指标
  9. 【Java进阶营】Java多线程基础学习(一)
  10. action与jsp传值的几种方法
  11. 豆瓣评论【数据集分享】
  12. 简单通用的Makefile编写例子
  13. SQL中笛卡尔积-cross join的用法
  14. STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app
  15. 华为鸿蒙荣耀壁纸,华为Mate 40全新主题、系统壁纸曝光:高清无水印
  16. SpringBoot使用Word导出表格
  17. proe服务器高速缓存位置,一招搞定Proe低版本打开高版本的问题 | 我爱分享网
  18. 人到中年,到底应该坚持打工还是去创业?
  19. mmWave SDK Module Documentation--Millimeter Wave(mmw) Demo for XWR18XX
  20. 高并发、高可用、高可靠微服务架构7大顶级设计思维模型

热门文章

  1. Pandas知识点-比较操作
  2. jquery插件合集之图片裁剪
  3. linux 分区 备份软件下载,硬盘分区备份(Image For Windows)
  4. leetcode - 621. 任务调度器
  5. 能量谱与功率谱(转自百度文库与维基百科)
  6. 数字图像处理--图像颜色
  7. 【OpenCV图像处理】一、图像相加、相减、相乘与相除的实现【转载】
  8. QT中QTableWidget清空或删除内容功能
  9. android画布原理,Android触摸事件如何实现笔触画布详解
  10. python中协程的理解_python协程的理解