A1067. Sort with Swap(0,*)
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,*)的更多相关文章
- 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 ...
- A1067 Sort with Swap(0, i) (25 分)
一.技术总结 题目要求是,只能使用0,进行交换位置,然后达到按序排列,所使用的最少交换次数 输入时,用数组记录好每个数字所在的位置. 然后使用for循环,查看i当前位置是否为该数字,核心是等待0回到自 ...
- PAT_A1067#Sort with Swap(0, i)
Source: PAT A1067 Sort with Swap(0, i) (25 分) Description: Given any permutation of the numbers {0, ...
- 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 ...
- 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 ...
- Pat1067:Sort with Swap(0,*)
1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...
- PAT1067. Sort with Swap(0, *) (25) 并查集
PAT1067. Sort with Swap(0, *) (25) 并查集 题目大意 给定一个序列, 只能进行一种操作: 任一元素与 0 交换位置, 问最少需要多少次交换. 思路 最优解就是每次 0 ...
- pat1067. Sort with Swap(0,*) (25)
1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...
- 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 ...
随机推荐
- Slurm任务调度系统部署和测试(源码)(1)
1. 概述1.1 节点信息2. 节点准备3. 部署NTP服务器4. 部署LDAP服务器5. 部署Munge认证服务6. 部署Mysql数据库服务7. 部署slurm7.1 创建slurm用户7.2 挂 ...
- Centos7 中文乱码
1. 安装中文库 yum groupinstall "fonts" 2. 检查是否有中文语言包 locale -a 3. 查看当前系统语言环境 locale 解析如下 LANG:当 ...
- github链接
github链接:https://github.com/bjing123 test1:https://github.com/bjing123/test-/blob/master/test1.t ...
- 『编程题全队』Beta 阶段冲刺博客二
1.提供当天站立式会议照片一张 2.每个人的工作 (有work item 的ID) (1) 昨天已完成的工作 孙志威: 1.添加了SubTask类,完成基本UI 2.为SubTask类添加了展开/收缩 ...
- RAID 磁盘阵列说明
Copy From wiki RAID档次 最少硬盘 最大容错 可用容量 读取性能 写入性能 安全性 目的 应用产业 单一硬盘 (引用) 0 1 1 1 无 JBOD 1 0 n 1 1 无( ...
- 软件工程_4th weeks
本周要进行阿尔法版本的发布,因此我们做了一些代码和测试方面的工作.当然了下了课后第一件事还是巩固课上讲的知识,比如MVP.四象限.看了演讲<最后一课>等. 一.结对编程 本周的结对编程继续 ...
- pandas.DataFrame
1.可以使用单个列表或列表列表创建数据帧(DataFrame). 单个列表 import pandas as pd data = [1,2,3,4,5] df = pd.DataFrame(data) ...
- DTD举例二
DTD举例二: <!--hbase语句库约束文件--> <!DOCTYPE hbaseGroup [ <!ELEMENT hbaseGroup (statements*)> ...
- CSS兼容性详解
前面的话 对于前端工程师来说,不想面对又不得不面对的一个问题就是兼容性.在几年之前,处理兼容性,一般地就是处理IE低版本浏览器的兼容性.而近几年,随着移动端的发展,工程师也需要注意手机兼容性了.本文将 ...
- appium学习记录1
xpath定位: 语法 driver.find_element_by_xpath("//android.widget.EditText[@index="登陆"/../pr ...