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. 基于Asp.Net Core Mvc和EntityFramework Core 的实战入门教程系列-5

    来个目录吧: 第一章-入门 第二章- Entity Framework Core Nuget包管理 第三章-创建.修改.删除.查询 第四章-排序.过滤.分页.分组 第五章-迁移,EF Core 的co ...

  2. ubuntu系统升级和其他相关操作记录

    之前在openstack中安装了ubuntu 12.04虚拟机,版本较低,需要升级为高版本.下面分享下升级过程: ubuntu系统升级操作:$ cat /etc/issueUbuntu 12.04.5 ...

  3. beta阶段测试基本概况对应机型硬件信息

    机型测试概况 测试结果 测试终端数 品牌分布分析 系统分布分析 分辨率分布 未执行 1 联想 4.0.3 480*800 安装失败 1 联想 4.2.1 480*854 通过 119 华为, 三星, ...

  4. 个人博客作业Week2(代码规范,代码复审)

    Q:是否需要有代码规范 首先我们来搞清楚什么是“代码规范”,它和“代码风格”又有什么关系.依据个人的审美角度,我可能更喜欢在函数与函数之间空出一行,可能在命名习惯和代码注释上更加的internatio ...

  5. Linux实践:模块

    标签(空格分隔): 20135321余佳源 一.实践原理 Linux模块是一些可以作为独立程序来编译的函数和数据类型的集合.之所以提供模块机制,是因为Linux本身是一个单内核.单内核由于所有内容都集 ...

  6. Linux内核分析(第八周)

    进程的切换和系统的一般执行过程 一.进程切换的关键代码switch_to分析 1.进程调度与其时机分析 分类: 第一种分类 I/O-bound:频繁的进行I/O:会花很多时间等待I/O操作完成 CPU ...

  7. 安装mysql解压版时遇到的错误

    平常都是使用exe安装mysql,今天使用解压包的,遇到了一系列问题: 环境:win10 mysql版本:mysql-5.7.21-winx64 开始一直用该网页的教程 https://www.cnb ...

  8. text2

    我的实践2代码已经传到GITHUb:https://github.com/jiaweihao/Test.git一下为测试结果截图:

  9. wuziqi

          五子棋结对人崔保雪的博客连接http://www.cnblogs.com/nuoxiaomi/ 题目简介 我们实现了一个五子棋的软件,该软件由初始化模块.下棋操作模块.人机对战模块.人人对 ...

  10. Practise 5.2测试与封装(黑白盒

    本次测试与封装(黑白盒). 结伴队友:叶子鹏,他的博客地址:http://www.cnblogs.com/kazehanaai/ 由于我们的程序从一开始就一起弄的,所以测试的话不好换伙伴,所以我的伙伴 ...