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

#!/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的更多相关文章

  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. Python之列表

    一.列表的特点 列表也是一种数据类型 列表元素是有序的,有编号的 列表元素的下标从0开始 列表中的每一个值叫一个元素,编号叫下标(索引/角标): stu_name=['崔海龙','杨帆','lrx', ...

  2. 自己实现数据结构系列四---Queue

    一.代码部分 1.定义接口: public interface Queue<E> { void enqueue(E e); E dequeue(); E getFront(); int g ...

  3. C#复习笔记(5)--C#5:简化的异步编程(异步编程的基础知识)

    异步编程的基础知识 C#5推出的async和await关键字使异步编程从表面上来说变得简单了许多,我们只需要了解不多的知识就可以编写出有效的异步代码. 在介绍async和await之前,先介绍一些基础 ...

  4. C#复习笔记(3)--C#2:解决C#1的问题(实现迭代器的捷径)

    实现迭代器的捷径 从这个题目上可以看到C#1实现一个迭代器模式的话是多么的痛苦,我自己也写过不下40行的代码来实现一个迭代器,C#中的迭代器模式是通过IEnumerable和他的泛型等价物IEnume ...

  5. composer 自动加载 通过classmap自动架子啊

    https://github.com/brady-wang/composer github地址 composer加载自己写的类 放入一个目录下 更改composer.json "autolo ...

  6. [官网]How to use distributed transactions with SQL Server on Docker

    How to use distributed transactions with SQL Server on Docker https://docs.microsoft.com/en-us/sql/l ...

  7. vue嵌套路由

    父组件  (注:to="/Flow/moban_a"这里不是文件加路径,是父组件路由+子组件路由) 路由配置

  8. 当mysql报错1045时的解决方法

    2.用记事本打开 添加 打开后,搜索mysqld关键字 找到后,在mysqld下面添加skip-grant-tables,保存退出. 如果保存在了c盘里不能修改那么就采用这样的方法 然后就可以修改c盘 ...

  9. MVP, MVC, MVVM, 傻傻分不清楚~

    1 简介 英文原文:MVC vs. MVP vs. MVVM 三者的目的都是分离关注,使得UI更容易变换(从Winform变为Webform),使得UI更容易进行单元测试. 2 MVC/MVP 2.1 ...

  10. Java 学习(1) ---JDK安装和配置环境变量

    一,Java 开发的第一步,就是安装JDK(Java Development ToolKit  Java开发工具包) JDK 是Java开发的核心,因为它包括Java 运行环境,工具包和命令.当我们安 ...