码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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. #include
    2. #include
    3. using namespace std;
    4. int n, a[100010], b[100010], ans, t, x, y, ind[100010], cnt, fIndex;
    5. bool flag;
    6. int main() {
    7. scanf("%d", &n);
    8. for (int i = 0; i < n; i++) {
    9. scanf("%d", &a[i]);
    10. b[i] = a[i];
    11. ind[a[i]] = i;
    12. }
    13. sort(b, b + n);
    14. for (int i = 1; i < n; i++) {
    15. if (a[i] != b[i]) {
    16. cnt++;//记录位置不对的个数
    17. }
    18. }
    19. while (true) {
    20. while (a[0] != 0) {
    21. t = ind[0]; //t是0的下标
    22. x = b[ind[0]]; //x是应该在此时0的位置的数
    23. y = ind[x]; //y是 应该在此时0的位置的数 的下标
    24. a[y] = 0;
    25. a[t] = x;
    26. ind[0] = y;
    27. ind[x] = t;
    28. ans++;
    29. cnt--;
    30. }
    31. flag = 1;
    32. if (cnt == 0) {//顺序不对的个数归零时排序已完成
    33. printf("%d", ans);
    34. break;
    35. }
    36. //此时a[0]=0,但是不一定完成移动,选择第一个顺序不对的数与0交换
    37. for (int i = fIndex; i < n; i++) {//fIndex记录第一个顺序不对的数的下标,及时更新
    38. if (a[i] != b[i]) {
    39. ind[0] = i;
    40. ind[a[i]] = 0;
    41. a[0] = a[i];
    42. a[i] = 0;
    43. ans++;
    44. flag = 0;
    45. fIndex = i;
    46. break;
    47. }
    48. }
    49. if (flag) {
    50. printf("%d", ans);
    51. break;
    52. }
    53. }
    54. return 0;
    55. }

     测试点12超时 参考博客:[PAT]1067 Sort with Swap(0, i) (25 分)(运行超时解决办法)_刘好念的博客-CSDN博客

  • 相关阅读:
    一个完整的初学者指南Django-part2
    CTF竞赛题目类型
    企业DevOps之路:Jenkins 流水线
    强化学习 double DQN 代码注释解析
    HTML做一个个人博客页面(纯html代码)
    继续来,同我一起撸Kotlin Channel 深水区
    JavaEE——Spring Boot + jwt
    算法秘籍-王一博 | 数据结构与算法
    为什么别人家的ChatGPT比我家的更聪明?
    基于springboot实现校园交友网站管理系统项目【项目源码+论文说明】
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/126137465
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号