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. Python-复习-习题-13

    复习 dict: dic = {'name':'alex'}增:dic['age'] = 21 存在就覆盖dic.setdefault() 存在什么也不做,没有就增加 删除:pop()按照key删除, ...

  2. apacheTomcat

    Window+R ------>cmd || Window PowerShell apacheTomcat\bin> ./startup.sh

  3. cordova打包webapp

    cordova打包webapp 在项目开发中,需要将h5页面打包成app,这个时候我们可以使用cordova来打包.在官方文档中,我们可以了解到创建一个app十分简单,你的电脑上有nodejs就行,我 ...

  4. js方法中拼接html时点击事件中拼接字符串参数

    1,代码 var html = '<a href="#" onclick="tableDelete(\''+ row.labelid +'\')"> ...

  5. layui使用记录

    一.layui表格渲染 如果后台返回的实力类里面包含另一个实体类,那么需要使用如下方式取出相应的值 var tableResult = table.render({ elem: '#' + Serve ...

  6. socket.io框架

    socket.io框架 一.问题背景 目前公司在互联网产品上需要程序与前端部分要进行一个实时交互,在进行一定程度上的选型后,决定使用socket.io框架进行一个实践,算是公司的一个新的 尝试,也算是 ...

  7. PHP 闭包获取外部变量和global关键字声明变量的区别

    最近在学习workerman的时候比较频繁的接触到回调函数,使用中经常会因为worker的使用方式不同,会用这两种不同的方式去调用外部的worker变量,这里就整理一下PHP闭包获取外部变量和glob ...

  8. PAT L2-021 点赞狂魔

    https://pintia.cn/problem-sets/994805046380707840/problems/994805058485469184 微博上有个“点赞”功能,你可以为你喜欢的博文 ...

  9. 转帖 云和恩墨 http://www.eygle.com/archives/2015/06/sql_version_count.html

    SQL多版本控制 - _CURSOR_OBSOLETE_THRESHOLD   作者:eygle |English [转载时请标明出处和作者信息]|[恩墨学院 OCM培训传DBA成功之道]链接:htt ...

  10. [转自知乎]飞腾国产CPU的部分知识

    1. 作者:常成链接:https://www.zhihu.com/question/48948852/answer/113595308来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...