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 ...
随机推荐
- 【kindle笔记】读书记录-总
注:谨以此贴记录各种读书心得的目录.包括纸质书和电子书等等. 我的啃豆 说起来,买啃豆的初心是<鸟哥私房菜> 来自亚马逊中国官网,于公元2017年,在双十一与双十二中间,一千大洋购得 有了 ...
- PropertyChangeSupport 监听器模式的应用
PropertyChangeSupport 类实现的监听器功能 ,它是java jdk自带的一个类,用于监听某个对象属性的改变来触发相应信息,具体看代码介绍 import java.beans.Pro ...
- [转帖]Vim 编辑器底端 [noeol], [dos] 的含义
Vim 编辑器底端 [noeol], [dos] 的含义 2012年11月28日 23:13:04 strongwangjiawei 阅读数:15484 https://blog.csdn.net/s ...
- .Net在操作mysql查询的时候出现“: Unknown column 'UserName' in 'where clause'”错误
今天使用.Net操作mysql查询的时候,如果加上条件查询的时候就会出现 Unknown column 'UserName' in 'where clause'这个错,不加条件直接select * f ...
- python爬虫之Splash使用初体验
Splash是什么: Splash是一个Javascript渲染服务.它是一个实现了HTTP API的轻量级浏览器,Splash是用Python实现的,同时使用Twisted和QT.Twisted(Q ...
- Django--CRM--菜单排序等
一 . 菜单排序 1.我们想把菜单排序.首先给菜单加上权重,权重大的排在上面, 这就要在菜单表上加上一个权重字段. 2. 我们在菜单表里面把权重改一下 3. 需要把权重字段的信息拿出来放到sessio ...
- 开源 DotNetty 实现的 Modbus TCP/IP 协议
本项目的目的是为了学习 DotNetty 与 Modbus 协议,参考 modjn 实现功能 0x01: Read Coils (读取线圈/离散量输出状态) 0x02: Read Discrete I ...
- HDU4341-Gold miner-分组DP
模拟黄金矿工这个游戏,给出每一个金子的位置和所需时间,计算在给定时间内最大收益. 刚看这道题以为金子的位置没什么用,直接DP就行,WA了一发终于明白如果金子和人共线的话只能按顺序抓. 这就是需要考虑先 ...
- Python中的numpy模块解析
numpy 1. 创建对象 维度(dimensions):轴 轴的个数:秩(rank) Numpy最重要的一个特点就是其N维数组对象(即ndarray) 创建数组最简单的函数就是用array函数: ...
- Codeforces1065F Up and Down the Tree 【树形DP】
推荐一道联赛练习题. 题目分析: 你考虑进入一个子树就可能上不来了,如果上得来的话就把能上来的全捡完然后走一个上不来的,所以这就是个基本的DP套路. 代码: #include<bits/stdc ...