码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • PAT Sort with Swap(0, i)


    1067 Sort with Swap(0, i)

    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:

    1. Swap(0, 1) => {4, 1, 2, 0, 3}
    2. Swap(0, 3) => {4, 1, 2, 3, 0}
    3. 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:

    1. 10
    2. 3 5 7 2 6 4 9 0 8 1

    Sample Output:

    9

    总结:自己是真滴蠢,怎么就是想不到怎么做呢???

    自己代码:

     题目中所给的例子是可以顺利执行的,但是 遇到 1 0 3 5 7 2 4 9 8这样顺序的,就出错了,早早的就结束了排序

    1. #include
    2. using namespace std;
    3. const int N=100010;
    4. int a[N],b[N];//a用来存储题目给出的数据的顺序,b存储a[i]在a数组中的位置
    5. int n;
    6. int main(){
    7. int cot=0;
    8. scanf("%d",&n);
    9. for(int i=0;i
    10. scanf("%d",&a[i]);
    11. b[a[i]]=i;
    12. }
    13. if(b[0]!=1){//如果0所在的位置不是1的话,需要把0交换到位置1
    14. int temp=a[1];
    15. swap(a[1],a[b[0]]);
    16. swap(b[0],b[temp]);
    17. cot++;
    18. }
    19. while(a[0]!=0){
    20. swap(a[b[b[0]]],a[b[0]]);
    21. b[0]=b[b[0]];
    22. cot++;
    23. }
    24. for(int i=0;iprintf("%d ",a[i]);
    25. puts("");
    26. printf("%d\n",cot);
    27. return 0;
    28. }

    还是多学习学习大佬的代码! 

    1. #include
    2. using namespace std;
    3. int main() {
    4. int n, t, cnt = 0, a[100010];
    5. cin >> n;
    6. for(int i = 0; i < n; i++){
    7. cin >> t;
    8. a[t] = i;
    9. }
    10. for(int i = 1; i < n; i++) {
    11. //这样的结构还可以顺便检验序列是否是递增
    12. if(i != a[i]) {
    13. while(a[0] != 0) {
    14. swap(a[0],a[a[0]]);
    15. cnt++;
    16. }
    17. if(i != a[i]) {//反例:3 0 2 1 4 //第一次循环后,a[0]==0 a[1]==1
    18. //需要继续往下循环,看看下面的数字是否是按序排列的
    19. swap(a[0],a[i]);
    20. cnt++;
    21. }
    22. }
    23. }
    24. cout << cnt;
    25. return 0;
    26. }

    好好学习,天天向上!

    我要考研!

  • 相关阅读:
    Spring Boot实现任意位置的properties及yml文件内容配置与获取
    Codeforces Round #803 (Div. 2) C. 3SUM Closure
    Go语言 切片 Slice
    浏览器原生JavaScript离线文字转语音TTS播放,支持Windows自带TTS语音和移动端(安卓、IOS)
    集货运输优化:数学建模步骤,Python实现蚁群算法(解决最短路径问题), 蚁群算法解决旅行商问题(最优路径问题),节约里程算法
    MySQL—多表查询(概述、基本实操、分类)
    基于SpringBoot的校园周边美食探索及分享平台的设计与实现
    Linux性能优化实战CPU篇(二)
    如何使用MISRA改进嵌入式编程
    构建用于签名/加密双证书测试体系的可执行命令
  • 原文地址:https://blog.csdn.net/weixin_50679551/article/details/127135203
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号