立志用最少的代码做最高效的表达


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 1

Sample 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相关推荐

  1. 1067 Sort with Swap(0, i) (25 分)【难度: 中 / 知识点: 置换群】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805403651522560 这种相关的题目见过很多次了. 常见的是只可以 ...

  2. 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 ...

  3. 【题意+分析】1071 Speech Patterns (25 分)_27行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 People often have a preference among synonyms of the same word. F ...

  4. 【题意分析】1024 Palindromic Number (25 分)_38行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A number that will be the same when it is written forwards or bac ...

  5. 【测试点三、四、五分析】1032 Sharing (25 分)_28行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 To store English words, one method is to use linked lists and sto ...

  6. 【详细分析】1023 Have Fun with Numbers (20 分)_20行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Notice that the number 123456789 is a 9-digit number consisting e ...

  7. 【测试点分析】1010 Radix (25 分)_37行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given a pair of positive integers, for example, 6 and 110, can th ...

  8. 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 ...

  9. PAT甲级1067 Sort with Swap(0, i):[C++题解]此题不是很懂!!

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析:y总从图论的角度来讲解的这道题,听得不是很懂. 此题不是很懂,暂留以后探讨.存在鸽的可能. ac代码 #include<bits ...

最新文章

  1. JIRA 6.3.6版本部署
  2. Java是否为整数_Java问题 输入一个数判定其是否为整数
  3. ECharts的点击事件
  4. Python Ctypes结构体指针处理(函数参数,函数返回)
  5. 论文小综 | Pre-training on Graphs
  6. java数组的用法_Java数组的使用
  7. 两种方法:实现输入一行字符,统计数字字符、英文字母和其他字符的个数(C语言)
  8. 自由职业者:5步拿下新项目
  9. 微软认知语音服务 语音识别
  10. 计算机四级(网络工程师)内容,计算机四级《网络工程师》考试内容
  11. 适配器(Adapter)
  12. matlab编译后方交会,摄影测量——后方交会(matlab
  13. python自动登录qq邮箱_python和selenium实现163邮箱自动登陆
  14. 開關(switch)規格中,SPDT, DPDT, 3PDT, 4PDT是什麼意思?
  15. mysql还原报错[err]1153解决办法
  16. Java利用数组求某年某日某月是某年的第几天(数组)
  17. AutoSAR系列讲解(入门篇)5.2-描述文件
  18. 喜欢花,喜欢海,喜欢日出和日落
  19. pandas实现分类汇总--小计,总计
  20. 计算机无法开机出现英文,电脑开机出现英文字母开不了机解决方法

热门文章

  1. WebRTC 的 log 系统实现分析
  2. 菜鸟网络技术专家:技术架构的战略和战术原则
  3. Kafka解析之topic创建(3)——合法性验证
  4. 动画图解 socket 缓冲区的那些事儿
  5. OS- -进程详详解
  6. 08 / LiveVideoStack主编观察:开源RTC的机会来了吗?
  7. 你有一个向LiveVideoStackCon讲师提问的机会
  8. JVM之XX参数详解
  9. 技术她力量,鹅厂女博士的寻“豹”之旅
  10. 一文读懂 @Decorator 装饰器——理解 VS Code 源码的基础