PAT1067. Sort with Swap(0, *) (25) 并查集
PAT1067. Sort with Swap(0, *) (25) 并查集
题目大意
给定一个序列, 只能进行一种操作: 任一元素与 0 交换位置, 问最少需要多少次交换.
思路
最优解就是每次 0 都和所在位置本应在的元素交换位置, 共 n - 1 次, 但是在交换中 0 可能会被交换到 0 号位置.
仔细思考一下, 其实每次换到 0 就是一个图的连通分量, 按照输入的次序和输入的值, 可以求出图共有多少个联通分量.
每个联通分量若要换回去, 需要 n - 1 次交换, 但是只能用 0 交换, 所以不含 0 且 结点个数不为 1 的连通分量需要进行 加 2 操作 (把 0 换进来, 排序, 再把 0 换出去).

代码
#include <cstdio>
#include <cstring>
#define MAXN 100100
using namespace std;
int arr[MAXN];
int cnt[MAXN];
int getFather(int a){
if(a == arr[a])
return a;
return arr[a] = getFather(arr[a]);
}
void merge(int a, int b){
a = getFather(a);
b = getFather(b);
if(a < b)
arr[b] = a;
else
arr[a] = b;
}
int main(){
int nNum;
scanf("%d", &nNum);
for(int i = 0; i < nNum; arr[i] = i++);
for(int i = 0; i < nNum; i++)
arr[i] = i;
for(int i = 0; i < nNum; i++){
int val;
scanf("%d", &val);
merge(i, val);
}
for(int i = 0; i < nNum; i++){
cnt[arr[i]]++;
}
int sum = 0;
for(int i = 0; i < nNum; i++){
if(arr[i] != i)
continue;
sum += cnt[i] - 1;
if(i != 0 && cnt[i] != 1)
sum += 2;
}
printf("%d", sum);
return 0;
}
PAT1067. Sort with Swap(0, *) (25) 并查集的更多相关文章
- pat1067. Sort with Swap(0,*) (25)
1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...
- 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 ...
- PAT 甲级 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 ea ...
随机推荐
- TOJ 4393 Game
描述 Bob always plays game with Alice.Today,they are playing a game on a tree.Alice has m1 stones,Bob ...
- 《springcloud 四》服务保护机制
服务保护机制SpringCloud Hystrix 微服务高可用技术 大型复杂的分布式系统中,高可用相关的技术架构非常重要. 高可用架构非常重要的一个环节,就是如何将分布式系统中的各个服务打造成高可用 ...
- Android模拟器访问本机服务器
Android模拟器访问本机服务器,用127.0.0.1访问不到,因为127.0.0.1已经被映射到模拟器了. 可以用以下两种方式访问 1. 用 10.0.2.2 2. 直接用 本机的IP地址,如:1 ...
- 接收时间戳model [JsonConverter(typeof(UnixDateTimeConverter))]
/// <summary> /// 创建时间 /// </summary> [JsonProperty("createtim ...
- Dictionary集合 字典
1 Dictionary<int, string> dic = new Dictionary<int, string>(); dic.Add(,"张三"); ...
- HDU 5007 字符串匹配
http://acm.hust.edu.cn/vjudge/contest/122814#problem/A 匹配到字符串就输出,水题,主要是substr的运用 #include <iostre ...
- Oracle JDBC 连接卡死后 Connection Reset
坑 这绝对是我碰计算机以来遇到的第一大坑! 症状: 在Linux主机上远程登录,执行一个简单的Oracle的JDBC连接程序(jar包),结果硬生生的卡在了连接建立验证阶段,然后等上几分钟后因为连接超 ...
- OLEDB 枚举数据源
在之前的程序中,可以看到有这样一个功能,弹出一个对话框让用户选择需要连接的数据源,并输入用户名和密码,最后连接:而且在一些数据库管理软件中也提供这种功能--能够自己枚举出系统中存在的数据源,同时还可以 ...
- mysql 字符串转换呈毫秒值
SELECT CEIL((UNIX_TIMESTAMP('2011-05-31 23:59:59') - UNIX_TIMESTAMP('2011-05-31 00:59:59'))/1000/60/ ...
- Android XMPP 例子(Openfire+asmack+spark) 出现登陆连接错误
Android XMPP 例子(Openfire+asmack+spark) 运行出来没问题,但是登陆的时候出现如下错误: 出现错误: 09-17 15:24:16.388: E/AndroidRun ...