题目

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 (<=105) 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与其他数字交换位置

解题思路

  1. 贪心算法:如果当前数字0在i号位置,找到数字i当前所处位置,将数字0和i交换

证明:

由于0必须参加交换操作,因此通过该策略,每步总是可以将一个非零的数回归本位。如果用0与其他不是该位置编号的数进行交换,显然会产生一个无效操作,因为后续操作中还是需要将刚才交换的数换回本位,因此该策略能将无效操作次数(与0交换的数没有回归本位的次数)降到最小,于是最优。

第一步: 0在7号位,因此将0与7交换,得到序列3502649781

第二步: 0在2号位,因此将0与2交换,得到序列3520649781

第三步: 0在3号位,因此将0与3交交换,得到序列0523649781

第四步: 0在0号位,因此将0与一个还未在本位的数字5交换,得到序列得到序列5023649781

第五步: 0在1号位,因此将0与1交换,得到序列5123649780

第六步: 0在9号位,因此将0与9交换,得到序列5123640789

第七步: 0在6号位,因此将0与6交换,得到序列5123046789

第八步: 0在4号位,因此将0与4交换,得到序列5123406789

第九步: 0在5号位,因此将0与5交换,得到序列0123456789

此时序列有序,总交换次数为9次。

//style="background-color: #FF8C00;"

  1. 定义数组int pos[N],记录数字所在位置(如:pos[i],表示数字i在pos[i]的位置)
  2. 哨兵为数字0,pos[0]=0,不一定排序已完成
    • pos[0]==0,但是排序未完成,将0需要与不在本位的数字交换(因为如果0与已回到本位的数字交换会导致交换次数变多),定义数字k初始化为1,记录当前不在本位的最小数字(避免每次都循环遍历数组,查找未回到本位的数字(复杂度为O(n^2)有两个点会超时))
    • pos[0]==0,已完成排序(如何检测已完成排序?--定义变量left记录除0以外不在自己本位数字的个数,left=0表示排序完成)

注意点

  1. 当0回到本位时,并不一定保证数字都已回到本位。此时需要找到一个未在本位的数字与0进行交换(如果选择已在本位的数字与0交换会导致交换次数增多)

Code

Code 01

#include <iostream>
#include <string>
using namespace std;
int main(int argc, char * argv[]) {
int N,m,cnt=0;//cnt--总交换次数
scanf("%d",&N);
int pos[N]= {0};// pos[i]--数字i的位置为pos[i]
int left=N-1; // 除0以外不在本位的数字
for(int i=0; i<N; i++) {
scanf("%d", &m);
pos[m]=i; // 数字m在位置i
if(m==i&&m!=0)left--; // 除0以外已在本位,left--
}
int k=1; //当前最小不在本位的数字,避免0在本位但排序未完成时,每次都需要遍历数组找未在本位的数字(复杂度O(n^2)会超时)
while(left>0) {
if(pos[0]==0) { //如果排序未完成,0已回到本位,将0与最小不在本位的k进行互换
while(k<N) {
if(pos[k]!=k) { //如果k不在本位
swap(pos[0],pos[k]);
cnt++; //无需left++,因为k本来就不再本位,0在本位但是left记录的是非0不在本位的数字个数
break;
}
k++; //如果k在本位,向前找最小不在本位的k
}
}
while(pos[0]!=0) { //0不在本位
swap(pos[0],pos[pos[0]]);
left--; //有一个数字回归本位
cnt++;
}
}
printf("%d",cnt);
return 0;
}

Code 02

#include <iostream>
#include <string>
using namespace std;
int main(int argc, char * argv[]) {
int N,m,cnt=0;
scanf("%d",&N);
int pos[N]= {0};
for(int i=0; i<N; i++) {
scanf("%d", &m);
pos[m]=i;
}
for(int i=0; i<N; i++) { //个人理解,这里处理不是很好,因为已遍历过的i不能保证其已回到本位
if(pos[i]!=i) {
while(pos[0]!=0) {
swap(pos[0],pos[pos[0]]);
cnt++;
}
if(pos[i]!=i) {
swap(pos[0],pos[i]);
cnt++;
}
}
}
printf("%d",cnt);
return 0;
}

PAT Advanced 1067 Sort with Swap(0,*) (25) [贪⼼算法]的更多相关文章

  1. PAT甲题题解-1067. Sort with Swap(0,*) (25)-贪心算法

    贪心算法 次数最少的方法,即:1.每次都将0与应该放置在0位置的数字交换即可.2.如果0处在自己位置上,那么随便与一个不处在自己位置上的数交换,重复上一步即可.拿样例举例:   0 1 2 3 4 5 ...

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

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

  4. 1067. Sort with Swap(0,*) (25)

    时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given any permutation of the num ...

  5. PAT (Advanced Level) 1067. Sort with Swap(0,*) (25)

    只对没有归位的数进行交换. 分两种情况: 如果0在最前面,那么随便拿一个没有归位的数和0交换位置. 如果0不在最前面,那么必然可以归位一个数字,将那个数字归位. 这样模拟一下即可. #include& ...

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

    1067. Sort with Swap(0,*) (25)   Given any permutation of the numbers {0, 1, 2,..., N-1}, it is easy ...

  7. pat1067. Sort with Swap(0,*) (25)

    1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...

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

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

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

随机推荐

  1. 将已有微信小程序转换为多端应用

    文档地址 https://nervjs.github.io/taro/

  2. vmware搭建linux环境

    参考博文:https://www.cnblogs.com/wuzm/p/10877990.html

  3. BurpSuite详解

    转载自:http://www.nxadmin.com/tools/689.html 本文由阿德马翻译自国外网站,请尊重劳动成果,转载注明出处 Burp Suite是Web应用程序测试的最佳工具之一,其 ...

  4. Maven:Unable to import maven project: See logs for details

    一.开发环境 idea2019.1 + apache-maven-3.6.2 + JDK 1.8.0_111 二.问题说明 导入maven 多模块工程之后,发现工程没有多模块的展开,而且也没有在 Ex ...

  5. GitHub 网站上不去/加载慢/加载不全 解决办法

    1. 当你打开你的 GitHub 2. F12 进入检查页面,点击 network 3. 找到变红的字段右键复制连接 4. 打开 DNS 查询网站,输入你复制的网址,点击查询 5. 选择国内的 ip ...

  6. 十二、CI框架之数据库查询

    一.在database文件中写明数据库相关信息 二.我们数据库中的表如图所示 三.在CI中查询数据库内容 四.在浏览器输出 不忘初心,如果您认为这篇文章有价值,认同作者的付出,可以微信二维码打赏任意金 ...

  7. HDU—4699 Editor 双向链表+子集和

    惨.今天聪哥选了2013 多校10做训练,结果一题都没做出来.这个题目是数据结构,正好是我强项 如果只是插入 删除 光标左右移动,那都小菜,用链表全解决,关键是那个Q k 要求 a1到aq的连续子序列 ...

  8. linux_c_tcp_example

    server #include <stdio.h> #include <stdlib.h> #include <string.h> #include <uni ...

  9. 用ps画一个Gif的小房子(1)

    效果如图: 制作方法: 1.新建200*200的画布:复制一块小房子图片 2.点击窗口-时间轴-勾选帧动画 3.如图所示(我这边是一帧对应一个图层) 4.新建图层-这边要新建24个图层,每个图层对应不 ...

  10. 图片分割之GrabCut算法、分水岭算法

    https://www.cnblogs.com/zyly/p/9392881.html 所谓图像分割指的是根据灰度.颜色.纹理和形状等特征把图像划分成若干互不交迭的区域,并使这些特征在同一区域内呈现出 ...