HR_Minimum Swaps 2
源代码超时 看了评论区改用了字典序列。
- #!/bin/python3
- import math
- import os
- import random
- import re
- import sys
- # Complete the minimumSwaps function below.
- def minimumSwaps(arr):
- # n = len(arr)
- # count = 0
- # for i in range(n):
- # if i > 0:
- # if i == arr[i-1]:
- # continue
- # else:
- # index = arr.index(i)
- # temp = arr[i-1]
- # arr[i-1] = arr[index]
- # arr [index] = temp
- # count += 1
- # return count
- ref_arr = sorted(arr)
- index_dict = {v: i for i,v in enumerate(arr)}
- swaps = 0
- for i,v in enumerate(arr):
- correct_value = ref_arr[i]
- if v != correct_value:
- to_swap_ix = index_dict[correct_value]
- arr[to_swap_ix],arr[i] = arr[i], arr[to_swap_ix]
- index_dict[v] = to_swap_ix
- index_dict[correct_value] = i
- swaps += 1
- return swaps
- if __name__ == '__main__':
- fptr = open(os.environ['OUTPUT_PATH'], 'w')
- n = int(input())
- arr = list(map(int, input().rstrip().split()))
- res = minimumSwaps(arr)
- fptr.write(str(res) + '\n')
- fptr.close()
HR_Minimum Swaps 2的更多相关文章
- [codeforces 339]E. Three Swaps
[codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a ...
- uva331 - Mapping the Swaps
Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entries in the ...
- UVA Mapping the Swaps
题目例如以下: Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entrie ...
- Three Swaps DFS
E. Three Swaps time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codefoces 432 C. Prime Swaps
哥德巴赫猜想: 任一大于2的偶数,都可表示成两个素数之和. 任一大于5的整数都可写成三个质数之和. 贪心取尽可能大的素数..... C. Prime Swaps time limit per test ...
- Swaps in Permutation
Swaps in Permutation You are given a permutation of the numbers 1, 2, ..., n and m pairs of position ...
- [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 ...
- [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 ...
- Codefoces 432C Prime Swaps(数论+贪心)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/26094917 题目连接:Codefoces ...
随机推荐
- Linux之磁盘挂载
1.查看磁盘分区情况: fdisk -l 可以看到,红框中的硬盘没有分区. 2.开始分区: fdisk /dev/vdb 3.格式化分区: mkfs.xfs 分区名 4.挂载磁盘 挂载方式1: 手动挂 ...
- Java 学习使用常见的开源连接池
目录 连接池介绍 自定义连接池 JDBC Tomcat Pool DBCP(DataBase Connection Pool) 使用配置文件来设置DBCP C3P0 Druid 连接池介绍 在说连接池 ...
- 【学习总结】Git学习-参考廖雪峰老师教程五-远程仓库
学习总结之Git学习-总 目录: 一.Git简介 二.安装Git 三.创建版本库 四.时光机穿梭 五.远程仓库 六.分支管理 七.标签管理 八.使用GitHub 九.使用码云 十.自定义Git 期末总 ...
- 10-vue的介绍
vue的作者叫尤雨溪,中国人.自认为很牛逼的人物,也是我的崇拜之神. 关于他本人的认知,希望大家读一下这篇关于他的文章,或许你会对语言,技术,产生浓厚的兴趣.https://mp.weixin.qq. ...
- SQL UPDATE with INNER JOIN
mysql - SQL UPDATE with INNER JOIN - Stack Overflowhttps://stackoverflow.com/questions/14491042/sql- ...
- 转:Linux下查看tomcat占用端口
https://blog.csdn.net/liufuwu1/article/details/71123597[root@server-crm mysql]# ps -ef | grep " ...
- spring AOP的用法
AOP,面向切面编程,它能把与核心业务逻辑无关的散落在各处并且重复的代码给封装起来,降低了模块之间的耦合度,便于维护.具体的应用场景有:日志,权限和事务管理这些方面.可以通过一张图来理解下: Spri ...
- Jenkins系统上的时间不正确问题
很简单,点击系统管理,选择执行脚本命令: 打开 [系统管理]->[脚本命令行]运行下面的命令 System.setProperty('org.apache.commons.jelly.tags. ...
- ES6/ES2015的一些特性的简单使
1.一些常用的ES6的特性: let, const, class, extends, super, arrow functions, template string, destructuring, d ...
- python之路--while, 格式化输出, 编码
一 . while循环 while 条件: 循环体(break, continue) while True: content = input('你想对我说什么:,输入你最帅退出') if conten ...