源代码超时 看了评论区改用了字典序列。

  1. #!/bin/python3
  2.  
  3. import math
  4. import os
  5. import random
  6. import re
  7. import sys
  8.  
  9. # Complete the minimumSwaps function below.
  10. def minimumSwaps(arr):
  11. # n = len(arr)
  12. # count = 0
  13. # for i in range(n):
  14. # if i > 0:
  15. # if i == arr[i-1]:
  16. # continue
  17. # else:
  18. # index = arr.index(i)
  19. # temp = arr[i-1]
  20. # arr[i-1] = arr[index]
  21. # arr [index] = temp
  22. # count += 1
  23. # return count
  24. ref_arr = sorted(arr)
  25. index_dict = {v: i for i,v in enumerate(arr)}
  26. swaps = 0
  27.  
  28. for i,v in enumerate(arr):
  29. correct_value = ref_arr[i]
  30. if v != correct_value:
  31. to_swap_ix = index_dict[correct_value]
  32. arr[to_swap_ix],arr[i] = arr[i], arr[to_swap_ix]
  33. index_dict[v] = to_swap_ix
  34. index_dict[correct_value] = i
  35. swaps += 1
  36.  
  37. return swaps
  38.  
  39. if __name__ == '__main__':
  40. fptr = open(os.environ['OUTPUT_PATH'], 'w')
  41.  
  42. n = int(input())
  43.  
  44. arr = list(map(int, input().rstrip().split()))
  45.  
  46. res = minimumSwaps(arr)
  47.  
  48. fptr.write(str(res) + '\n')
  49.  
  50. fptr.close()

HR_Minimum Swaps 2的更多相关文章

  1. [codeforces 339]E. Three Swaps

    [codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a ...

  2. uva331 - Mapping the Swaps

    Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entries in the ...

  3. UVA Mapping the Swaps

    题目例如以下: Mapping the Swaps  Sorting an array can be done by swapping certain pairs of adjacent entrie ...

  4. Three Swaps DFS

    E. Three Swaps time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. Codefoces 432 C. Prime Swaps

    哥德巴赫猜想: 任一大于2的偶数,都可表示成两个素数之和. 任一大于5的整数都可写成三个质数之和. 贪心取尽可能大的素数..... C. Prime Swaps time limit per test ...

  6. Swaps in Permutation

    Swaps in Permutation You are given a permutation of the numbers 1, 2, ..., n and m pairs of position ...

  7. [Swift]LeetCode801. 使序列递增的最小交换次数 | Minimum Swaps To Make Sequences Increasing

    We have two integer sequences A and B of the same non-zero length. We are allowed to swap elements A ...

  8. [LeetCode] Minimum Swaps To Make Sequences Increasing 使得序列递增的最小交换

    We have two integer sequences A and B of the same non-zero length. We are allowed to swap elements A ...

  9. Codefoces 432C Prime Swaps(数论+贪心)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/26094917 题目连接:Codefoces ...

随机推荐

  1. Linux之磁盘挂载

    1.查看磁盘分区情况: fdisk -l 可以看到,红框中的硬盘没有分区. 2.开始分区: fdisk /dev/vdb 3.格式化分区: mkfs.xfs 分区名 4.挂载磁盘 挂载方式1: 手动挂 ...

  2. Java 学习使用常见的开源连接池

    目录 连接池介绍 自定义连接池 JDBC Tomcat Pool DBCP(DataBase Connection Pool) 使用配置文件来设置DBCP C3P0 Druid 连接池介绍 在说连接池 ...

  3. 【学习总结】Git学习-参考廖雪峰老师教程五-远程仓库

    学习总结之Git学习-总 目录: 一.Git简介 二.安装Git 三.创建版本库 四.时光机穿梭 五.远程仓库 六.分支管理 七.标签管理 八.使用GitHub 九.使用码云 十.自定义Git 期末总 ...

  4. 10-vue的介绍

    vue的作者叫尤雨溪,中国人.自认为很牛逼的人物,也是我的崇拜之神. 关于他本人的认知,希望大家读一下这篇关于他的文章,或许你会对语言,技术,产生浓厚的兴趣.https://mp.weixin.qq. ...

  5. SQL UPDATE with INNER JOIN

    mysql - SQL UPDATE with INNER JOIN - Stack Overflowhttps://stackoverflow.com/questions/14491042/sql- ...

  6. 转:Linux下查看tomcat占用端口

    https://blog.csdn.net/liufuwu1/article/details/71123597[root@server-crm mysql]# ps -ef | grep " ...

  7. spring AOP的用法

    AOP,面向切面编程,它能把与核心业务逻辑无关的散落在各处并且重复的代码给封装起来,降低了模块之间的耦合度,便于维护.具体的应用场景有:日志,权限和事务管理这些方面.可以通过一张图来理解下: Spri ...

  8. Jenkins系统上的时间不正确问题

    很简单,点击系统管理,选择执行脚本命令: 打开 [系统管理]->[脚本命令行]运行下面的命令 System.setProperty('org.apache.commons.jelly.tags. ...

  9. ES6/ES2015的一些特性的简单使

    1.一些常用的ES6的特性: let, const, class, extends, super, arrow functions, template string, destructuring, d ...

  10. python之路--while, 格式化输出, 编码

    一 . while循环 while 条件: 循环体(break, continue) while True: content = input('你想对我说什么:,输入你最帅退出') if conten ...