7-16 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个数字的排列由若干独立的环组成的知识。

1.单元环swap(0,i)次数为0;

2.有0的非单元环,swap(0,i)次数为n-1;

3.无0的单元环,可以先把环里随便一个数和0交换一次,这样整个环就变成了含0的n+1个元素的非单元环,根据前面的情况2,次数为(n+1)-1,但还要加上把0换进去的那次,故swap(0,i)次数为n+1;

代码如下:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int flag=0; // 记录circle是否含0;
int main(){int N; cin>>N;// 储存数列vector<int> sequence; // 为了已知元素找下标而建立的position集合// 其中pos[i]表示sequence中数值为i的元素的下标为pos[i]vector<int> pos;   sequence.resize(N); // 不能少,为sequence创造N个空间pos.reserve(N); // 不能少//读入数据和初始化posfor(int i=0;i<N;i++) {scanf("%d",&sequence[i]);pos[sequence[i]]=i;}//temp储存circle中第一个位置的元素//swap_times记录swap(0,i)的次数//elements_num记录circle中元素的个数int temp,swap_times=0,elements_num=0;for(int element=0;element<N;element++){//temp0用来进行下面的赋值活动int element_circle=element; int temp0;temp=sequence[element_circle];//每次进入circle前初始化elements_num为0;elements_num=0; //若为单元环,则不进入while,故elements_num为0;while(pos[element_circle]!=element_circle){//判断当前位置sequence[i]是否为i,即是否是单元环和环是否结束;若不是,则继续if(sequence[element_circle]==0)        flag=1;//记录该circle是否有0 elements_num++; // 记录circle元素个数if(pos[pos[element_circle]]!=pos[element_circle])//用于判断当前元素是否环的尾巴sequence[element_circle]=element_circle; //不是尾巴elsesequence[element_circle]=temp;// 是尾巴,用temp来赋值//下面部分用来更新pos和移动element_circletemp0=element_circle;element_circle=pos[element_circle];pos[temp0]=temp0;//}if(elements_num!=0){ // 判断是否是单元环if(flag==1) swap_times+=elements_num-1; //判断circle是否有0,有0的非单元环移动elements-1次else swap_times+=elements_num+1; //无0的非单元环移动elements+1次}//归零flag和elements_numflag=elements_num=0;}cout<<swap_times<<end;return 0;
}

转载于:https://www.cnblogs.com/A-Little-Nut/p/8067017.html

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

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

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

  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. PAT A1067 Sort with Swap(0, i) ——天街小雨润如酥,草色遥看近却无

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

  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. 1067. Sort with Swap(0,*)

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

  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. python for bioinformatics相关题目
  2. 样式表中的 element.style样式如何修改
  3. 请谈一下Spring MVC的工作原理是怎样的?
  4. 微服务中远程调用Dubbo与Feign对比
  5. conda重命名环境env
  6. Hadoop集群的kerberos认证
  7. java 动态加载控件_JS动态添加节点后渲染为EasyUI控件,EasyUI动态渲染解析解决方案...
  8. Fiddler及浏览器开发者工具进行弱网测试
  9. LeetCode 38外观数列
  10. 二十、Java基础--------IO流之其他对象
  11. c语言刷屏函数的作用是什么,刷屏神器源码(C语言控制台版)【原创】
  12. 软考初级程序员常见类型题,错题个人笔记
  13. Matlab Gramm绘图工具箱
  14. 不那么完美的 RSS 订阅方案 — feedly + RSSHub Radar
  15. [Java] 编码规范与基本概念
  16. 如何将bmp转化为jpg?
  17. android渠道首发规则,酷传推广手册Android渠道首发规则.doc
  18. A320M HDV 4.0主板用CH341A手动刷BIOS支持5600g
  19. 如何把云服务器恢复到最原始的状态
  20. ubuntu18.04系统安装+基本环境配置【原创】

热门文章

  1. 有的字体,用黑色渲染,效果是灰色
  2. BAT中一行太长,如何折行
  3. 解决办法:VirtualBox只能安装32位的问题
  4. LINUX安装C#开发环境
  5. 为不干活的员工辩护,要小心
  6. C调用系统命令ping崩溃日志
  7. 管理感悟:说说NWT裁员的经历及关键错误
  8. 多任务计时器anytime
  9. 为什么很少人学汇编_为什么那么多人学模具,成功的只有不到5%???
  10. python turtle代码示例-Python turtle.left方法代码示例