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
 #include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int num[];
int main(){
int N, temp, index = , ans = , cnt = ;
scanf("%d", &N);
for(int i = ; i < N; i++){
scanf("%d", &temp);
num[temp] = i;
if(temp != && temp != i)
cnt++;
}
while(cnt > ){
if(num[] == ){
for(int i = index; i < N; i++){
if(num[i] != i){
index = i;
break;
}
}
swap(num[], num[index]);
ans++;
continue;
}else{
swap(num[], num[num[]]);
ans++;
cnt--;
}
}
printf("%d", ans);
cin >> N;
return ;
}

总结:

1、题意:按照示例所说,用0与其他元素交换位置,使得被交换的元素到达正确的位置。不断进行,直到所有元素都归位。

2、在0与其他元素交换的过程中,会出现0被交换到0的位置,后续无法继续交换的情况,但有可能整个序列还未调整完。这时需要找一个未归位的元素,将其与0交换,使0乱序,再继续正常进行交换流程。找未归位元素的过程不能每次都从头开始,否则复杂度会变为n^2。可以设置一个下标index初值为1,其左边的元素都已经有序,所以每次只需从index开始寻找。

3、设置一个计数器,该计数器记录当前不在本位的元素的个数(初始化可在读入数据时完成)。进行一次交换并有一个元素归位时计数器减一。当计数器为0时完成排序。

4、在num[ ]数组中,如果用数组内容表示数字,数组下标表示位置,则每次0与一个元素交换时,都需要遍历一次数组以找到该元素的位置,导致复杂度为n^2。但如果用数组下标表示数字,数组内容表示数字的位置,则可以避免这种情况。

5、当输入的数据过万时,就要注意避免n^2复杂度。

A1067. Sort with Swap(0,*)的更多相关文章

  1. PAT甲级——A1067 Sort with Swap(0, i)

    Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...

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

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

  3. PAT_A1067#Sort with Swap(0, i)

    Source: PAT A1067 Sort with Swap(0, i) (25 分) Description: Given any permutation of the numbers {0, ...

  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,*)

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

  6. Pat1067:Sort with Swap(0,*)

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

  7. PAT1067. Sort with Swap(0, *) (25) 并查集

    PAT1067. Sort with Swap(0, *) (25) 并查集 题目大意 给定一个序列, 只能进行一种操作: 任一元素与 0 交换位置, 问最少需要多少次交换. 思路 最优解就是每次 0 ...

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

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

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

随机推荐

  1. SpringBoot日记——Spring的安全配置-登录认证与授权

    安全是每个项目开发中都需要考虑的,比如权限控制,安全认证,防止漏洞攻击等. 比较常见的安全框架有:Apache的shiro.Spring Security等等,相信用shiro的用户群体更多,而sec ...

  2. python之requests

    发送请求 导入 Requests 模块: >>> import requests >>> r = requests.get('https://xxxxxxx.jso ...

  3. 安装Visual Studio2013

    安装Visual Studio2013现在官网下载在选择安装的可选功能这里,大家可以根据自己需要勾选,也可以默认全选.这里有个小功能,把鼠标放在文字上,会弹出各个功能的详细描述.选择四个常用的功能,另 ...

  4. 《Linux内核分析》 期中总结

    Linux内核分析 期中总结 20135307 张嘉琪 一.Linux内核分析课程总结 学习笔记汇总 第一节 计算机是如何工作的 第二节 操作系统是如何工作的 第三节 构造一个简单的Linux系统Me ...

  5. <构建之法>第三10、11、12章

    第十章 典型用户和场景 问题:如何更准确知道用户的需求是什么,设计出满足用户的软件? 第十一章 软件设计与实现 问题:软件设计过程中,如何管理设计变更? 第十二章 用户体验 问题:在何时开始设计用户体 ...

  6. Daily Scrum- 12/23

    Meeting Minutes 与Travis和Zhongqiu讨论了Beta的Feature以及更长期的计划: 讨论了一些使用及设计上的Bug (Feature); 开始了新的开发周期: Burnd ...

  7. CentOS7 卸载mariadb 安装mysql的过程:

    1. 检查安装的mariadb rpm -qa |grep mariadb 得到已经安装的安装包 mariadb-libs-5.5.56-2.el7.x86_64mariadb-devel-5.5.5 ...

  8. K3CLOUD安装教程

    1.安装SQLSERVER2008 2.安装K3CLOUD安装包,此处各种安装iis,tomcat,ftp等环境,有过it经验的应该都能自己搞定,不详细赘述 3.进入管理中心,进行设置,默认为127. ...

  9. fasterxml.jackson 将对象转换为json报错处理

    最近在做查询的数据遇到如下报错: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found ...

  10. 关于miniconda的安装,配置以及包批量安装和使用

    由于时间很晚了.就不写废话了. conda官方文档地址:http://conda.pydata.org/docs/ 一切其实都可以从miniconda的文档找到,这里只纪录自己操作的时候遇到的值得一说 ...