pat1067. Sort with Swap(0,*) (25)
1067. Sort with Swap(0,*) (25)
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
思路:将序列转换为图,形成键-值关系
例如样例:3 5 7 2 6 4 9 0 8 1
对应的值:0 1 2 3 4 5 6 7 8 9
对应的键:3 5 7 2 6 4 9 0 8 1
发现3类环:
a.元素个数>1,包含0的环:0-7-2-3-0
b.元素个数>1,不包含0的环:1-9-6-4-5-1
c.元素个数==1的环:8
其实一张图无非也就最多存在上述3类环。
对于a:交换次数=环的元素个数-1
对于b:交换次数=环的元素个数+1
对于c:交换次数=0
所以,只要统计元素个数>1的环的情况即可。然后根据这些元素个数>1的环是否包含元素0来计算总的交换次数。
代码如下:
#include<cstdio>
#include<stack>
#include<cstring>
#include<iostream>
#include<stack>
#include<set>
#include<map>
using namespace std;
map<int,int> ha;
bool vis[];
void DFS(int cur,int s){
vis[cur]=true;
if(ha[cur]==s){
return;
}
DFS(ha[cur],s);
}
//统计点的数量>1的环的个数,有0在的换交换次数是个数-1;没有0在的环,交换次数是个数+1
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int count,n;
scanf("%d",&n);
count=n;
int i,num;
for(i=;i<n;i++){
scanf("%d",&num);
if(num==i){//去掉元素个数为0的环,剩下的元素个数
vis[num]=true;
count--;
}
ha[num]=i;
}
//剩下的环除了包含元素0的环,其他环的交换次数=环自身元素个数+1
for(i=;i<n;i++){
if(!vis[i]){
DFS(i,i);
count++;
}
}
if(ha[]!=){//如果0不单独成环,说明有元素个数>1的环包含0,则这个环在34行时多加了2,这里减去
count-=;
}
printf("%d\n",count);
return ;
}
pat1067. Sort with Swap(0,*) (25)的更多相关文章
- PAT1067. Sort with Swap(0, *) (25) 并查集
PAT1067. Sort with Swap(0, *) (25) 并查集 题目大意 给定一个序列, 只能进行一种操作: 任一元素与 0 交换位置, 问最少需要多少次交换. 思路 最优解就是每次 0 ...
- Pat1067:Sort with Swap(0,*)
1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...
- 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 ...
- 1067. Sort with Swap(0,*) (25)
时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given any permutation of the num ...
- PAT Advanced 1067 Sort with Swap(0,*) (25) [贪⼼算法]
题目 Given any permutation of the numbers {0, 1, 2,-, N-1}, it is easy to sort them in increasing orde ...
- PAT (Advanced Level) 1067. Sort with Swap(0,*) (25)
只对没有归位的数进行交换. 分两种情况: 如果0在最前面,那么随便拿一个没有归位的数和0交换位置. 如果0不在最前面,那么必然可以归位一个数字,将那个数字归位. 这样模拟一下即可. #include& ...
- PAT甲题题解-1067. Sort with Swap(0,*) (25)-贪心算法
贪心算法 次数最少的方法,即:1.每次都将0与应该放置在0位置的数字交换即可.2.如果0处在自己位置上,那么随便与一个不处在自己位置上的数交换,重复上一步即可.拿样例举例: 0 1 2 3 4 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 ...
- 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 ...
随机推荐
- JavaScript-Tool-富文本:UEditor
ylbtech-JavaScript-Tool-富文本:UEditor UEditor是由百度WEB前端研发部开发的所见即所得的开源富文本编辑器,具有轻量.可定制.用户体验优秀等特点.开源基于BSD协 ...
- oracle--循环PL/SQL--demo1---
--简单的条件判断if–then --编写一个过程,可以输入一个雇员名,如果该雇员的工资低于2000,就给该员工工资增加10%. create or replace procedure sp_pro6 ...
- IIS应用池保持激活工具开发
之前的 开源一个定时任务调度器 webscheduler 中涉及的定时调用应用已经开始在正式环境中启用,发布到IIS下进行测试,发现一旦应用长时间没有访问(大约半个多小时)就会引发 Applicati ...
- python 爬虫 之BeautifulSoup
BeautifulSoup是一个模块,该模块用于接收一个HTML或XML字符串,然后将其进行格式化,之后便可以使用他提供的方法进行快速查找指定元素,从而使得在HTML或XML中查找指定元素变得简单. ...
- 2017 world final
E 解题关键:二分时注意C函数的单调性. #include<bits/stdc++.h> #define eps 1e-8 #define INF 0x3f3f3f3f using nam ...
- pig flatten
今天通过不断的尝试,终于知道这个flatten的用法了.其实吧,有时候关键是要test,才能充分理解解说.不过,同事给说的有点问题,误导了我.整的我一直没明白怎么回事. 这是官方的解释: The FL ...
- contentType和dataType的区别
contentType: 告诉服务器,我要发什么类型的数据 dataType:告诉服务器,我要想什么类型的数据,如果没有指定,那么会自动推断是返回 XML,还是JSON,还是script,还是Stri ...
- CSS学习系列1 - CSS中的盒子模型 box model
css中有一个盒子模型的概念. 主要是用来告诉浏览器如何来计算页面元素的宽度和高度, 比如该元素的宽度/高度 是否包括内边距,边框,外边距. 盒子模型有一个属性box-sizing属性来说明是否包括 ...
- 20170527关于Thingking in Java
由于工作上的需求,要开始学习Android开发,所以先开始看一些Java方面的知识.学习从Thingking in Java开始,看了一下第一张,感觉真的是一本好书,希望自己努力把他看完,加油! 第一 ...
- DataSet,DataTable,DataColumn,DataRow的常用操作
DataSet 这个玩意没什么好讲的,当ado.net查询出有多张表集合的数据返回时,这个时候就会使用到DataSet. DataTable //表之间直接赋值 dt2=dt1; 两者指向同一内存空间 ...