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 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
经过演算发现有 0 的环是 n-1 没0的环是n+1当然了 个数要大于等于2才行
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int digit[maxn], pre[maxn];
int cnt[maxn];
int find(int x){if(x == pre[x])return x;return pre[x] = find(pre[x]);
}
void merge(int x, int y)
{int fx = find(x);int fy = find(y);if(fx != fy){pre[fx] = fy;}
}
int main()
{ios::sync_with_stdio(false);int n;cin>>n;for(int i = 0; i < n ; i++){cin>>digit[i]; pre[i] = i;} for(int i = 0 ; i < n; i ++)merge(i , digit[i]);for(int i = 0; i < n; i ++)cnt[find(i)] ++; int gg = cnt[find(0)];int kk = find(0);int ans = 0;if(gg != 1) //表明不是digit[0] = 0 ans = gg - 1; for(int i = 0 ; i < n ; i++){if(cnt[i] == 1 || i == kk || cnt[i] == 0)continue;ans = ans + cnt[i] + 1; } cout<<ans<<endl;return 0;
}
1067 Sort with Swap(0, i) (25 分)思路相关推荐
- 【题意+分析】1067 Sort with Swap(0, i) (25 分)_24行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given any permutation of the numbers {0, 1, 2,-, N−1}, it is easy ...
- 1067 Sort with Swap(0, i) (25 分)【难度: 中 / 知识点: 置换群】
https://pintia.cn/problem-sets/994805342720868352/problems/994805403651522560 这种相关的题目见过很多次了. 常见的是只可以 ...
- 10-排序6 Sort with Swap(0, i) (25 分)
Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...
- 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, i):[C++题解]此题不是很懂!!
文章目录 题目分析 题目来源 题目分析 来源:acwing 分析:y总从图论的角度来讲解的这道题,听得不是很懂. 此题不是很懂,暂留以后探讨.存在鸽的可能. ac代码 #include<bits ...
- 1067. Sort with Swap(0,*)
好弱啊,这个题目折腾了好久. 构造hash表用来维护各个元素所在的位置,利用map维护一颗红黑树来保存还未确定的元素的位置. (1)用0不断的跟其他元素交换,每次交换都会保证一个元素在正确的位置. ( ...
- PTA 1067 Sort with Swap(0, i) (25 分)(思维)
传送门:点我 Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasin ...
- PAT A1067 Sort with Swap(0, i) ——天街小雨润如酥,草色遥看近却无
PAT A1067 Sort with Swap(0, i) 本题使用了姥姥教的方法,通过交换过程(第一个开始动的元素,通过一系列交换到达自己应该在的位置)可以发现他们形成了一个闭环,大家手拉手,每个 ...
- 算法 排序6 Sort with Swap(0, i) 2013年免试研究生上机考试真题
全部每周作业和视频思考题答案和解析 见 浙江大学 数据结构 思考题+每周练习答案 题目:Given any permutation of the numbers {0, 1, 2,..., N−1}, ...
- 7-16 Sort with Swap(0, i) | PTA数据结构与算法——C语言实现
2013年浙江大学免试研究生上机考试真题. 原题链接:PTA | 程序设计类实验辅助教学平台 题目描述 给定包含数字 {0, 1, 2,..., N−1} 的任一排列,很容易对它们进行升序排序. 但是 ...
最新文章
- Linux开启路由转发功能(透明代理环境搭建)
- awk 内嵌正则 提取字符串_干货-Shell编程文本处理三剑客之-awk
- 软件工程--第五章-- 总体设计
- c语言中如何取消最后一个空格,新人提问:如何将输出时每行最后一个空格删除...
- Hibernate映射关系之多对多
- php pathinfo()函数
- QtCreator导入yocto sdk记录
- Industrial Design System v4.5 1CD(工业产品设计软件)
- Error-Input tensor has type kTfLiteFloat32: it requires specifying NormalizationOptions metadata to
- window.dialogArguments.location.reload();
- Free Pascal初次体验(有亮点哦)
- matlab电磁场与微波技术相关仿真的代码,几乎覆盖电磁和微波领域
- youtube的使用体会
- UPC6605 所罗门王的宝藏
- 将你的笔记本变成无线路由器
- 汽车学堂 自 动 驾 驶 决 策 与 控 制 算 法——现 代 控 制 理 论学习(一)
- 实时级嵌入式系统半实物仿真测试平台系统描述
- 图基准数据集(OGB)
- vue2进阶篇:vue-router之“使用独享路由守卫”
- IOS keychain 使用