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

注意:写题前一定要注意数组下标和对应内容的关系,然后循环体内的每一个变量的判定一定要仔细思考,这里一直卡在while(k<n)这里,没写一步循环将无限进行下去。使用swap函数时一定要加上头文件algorithm和using namespace std;

具体代码

#include <cstdio>
#include <algorithm>
using namespace std;
int main(){int n;scanf("%d",&n);int no=n-1;int pos[100010];int num;for(int i=0;i<n;i++){scanf("%d",&num);if(num==i&&num!=0)no--;pos[num]=i;//no用于存放除0外未回到原位的数字的数量}int k=1;int ans=0;while(no>0){//当还有未回归到原位的数字时if(pos[0]==0){while(k<n){if(pos[k]!=k){swap(pos[0],pos[k]);ans++;break;}k++;}}while(pos[0]!=0){swap(pos[0],pos[pos[0]]);ans++;no--;}
}
printf("%d\n",ans);
return 0;
}

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

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

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

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

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

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

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

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

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

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

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

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

最新文章

  1. [C#基础]Func和Action学习
  2. 【深入Java虚拟机】之四:类加载机制
  3. 看门狗寄存器c语言代码_「正点原子NANO STM32F103开发板资料连载」第十一章 看门狗实验...
  4. php.ini icuuc54.dll,icuuc54.dll下载|
  5. Jsp Servlet 个人博客
  6. jmeter测试tomcat并发
  7. linux存放日志的目录下,linux系统各种日志存储路径和详细介绍
  8. 深信服SCSA认证复习笔记三
  9. 三角函数泰勒级数推导
  10. USB转RS232串口应用
  11. 大师级游戏建模教程:使用Maya和XGen进行角色制作
  12. 毕业论文尾注和参考文献
  13. 微信公众号支付、支付查询、退款、退款查询、转账到零钱银行卡、转账查询接口整合(V2)
  14. segment:?co?_如何跟踪用户动作并了解它们:Segment + MixPanel
  15. Frontiers in Pharmacology2020 | MOSES+:分子生成模型的benchmark平台
  16. C++逆序数(奇排列和偶排列的判定)
  17. 免签微信 HOOK 最新版 7.0.3微信
  18. dorado java_概述-Dorado JDBC Addon
  19. 银行家算法原理及代码实现
  20. 真肝,整理了一周的Spring面试大全【含答案】,吊打Java面试官

热门文章

  1. 《位置大数据隐私管理》—— 1.5 典型的位置隐私保护技术
  2. 设置UITextField键盘上return key不可点击
  3. 简单实现ToolStripMenuItem(菜单栏)的单选效果
  4. Varnish 安装部署
  5. H3C模拟器simware搭建总结
  6. 数据结构——结构体总结
  7. vue-cli 打包出来的文件缺少_Vue cli构建 及 项目打包以及出现的问题 (update 2020-05)...
  8. log4j配置文件,日志文件的模板
  9. Mybatis的xml映射文件,sql的模糊查询的实现(两种方式)占位符拼接,concat关键字
  10. 如何使用String获取字符串中某一个字符后面的所有字符?