【题意+分析】1067 Sort with Swap(0, i) (25 分)_24行代码AC
立志用最少的代码做最高效的表达
PAT甲级最优题解——>传送门
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 (≤10^5) 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 1Sample Output:
9
题意:每次只能以0所在的位置和某数交换,若想变为一个有序数列,问最小交换次数是多少。
算法逻辑:从贪心的角度看, 每次都将0所在位置和一个不在正确位置的数字交换显然为最优解。 那么,如果某次交换后,0回到了位置0上,就将其与随便一个不在正确位置上的数字交换即可。 其中贪心思想请读者仔细体会。
#include<bits/stdc++.h>
using namespace std;
int pos[100010];
int main() {int n; cin >> n;for(int i = 0; i < n; i++) {int x; cin >> x;pos[x] = i;}int cnt = 0; for(int i = 0; i < n; i++) if(pos[i] != i) {while(pos[0] != 0) {swap(pos[0], pos[pos[0]]); cnt++;}if(pos[i] != i) {swap(pos[0], pos[i]);cnt++;}}cout << cnt; return 0;
}
耗时:
求赞哦~ (✪ω✪)
【题意+分析】1067 Sort with Swap(0, i) (25 分)_24行代码AC相关推荐
- 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 ...
- 【题意+分析】1071 Speech Patterns (25 分)_27行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 People often have a preference among synonyms of the same word. F ...
- 【题意分析】1024 Palindromic Number (25 分)_38行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A number that will be the same when it is written forwards or bac ...
- 【测试点三、四、五分析】1032 Sharing (25 分)_28行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 To store English words, one method is to use linked lists and sto ...
- 【详细分析】1023 Have Fun with Numbers (20 分)_20行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Notice that the number 123456789 is a 9-digit number consisting e ...
- 【测试点分析】1010 Radix (25 分)_37行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given a pair of positive integers, for example, 6 and 110, can th ...
- 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 ...
最新文章
- JIRA 6.3.6版本部署
- Java是否为整数_Java问题 输入一个数判定其是否为整数
- ECharts的点击事件
- Python Ctypes结构体指针处理(函数参数,函数返回)
- 论文小综 | Pre-training on Graphs
- java数组的用法_Java数组的使用
- 两种方法:实现输入一行字符,统计数字字符、英文字母和其他字符的个数(C语言)
- 自由职业者:5步拿下新项目
- 微软认知语音服务 语音识别
- 计算机四级(网络工程师)内容,计算机四级《网络工程师》考试内容
- 适配器(Adapter)
- matlab编译后方交会,摄影测量——后方交会(matlab
- python自动登录qq邮箱_python和selenium实现163邮箱自动登陆
- 開關(switch)規格中,SPDT, DPDT, 3PDT, 4PDT是什麼意思?
- mysql还原报错[err]1153解决办法
- Java利用数组求某年某日某月是某年的第几天(数组)
- AutoSAR系列讲解(入门篇)5.2-描述文件
- 喜欢花,喜欢海,喜欢日出和日落
- pandas实现分类汇总--小计,总计
- 计算机无法开机出现英文,电脑开机出现英文字母开不了机解决方法