HR_Minimum Swaps 2

时间:2022-07-19 00:15:46

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

#!/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. MVC学习地址

    http://www.cnblogs.com/n-pei/tag/Asp.net%20MVC/

  2. 使用EntityFramework6连接MySql数据库

    准备工具: VS2013.MySQL For VisualStudio 1.1.4.Connector/Net 6.8.3(百度网盘里) 程序包管理器执行命令: Install-Package Ent ...

  3. 回文串---吉哥系列故事——完美队形II

    HDU  4513 Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出 ...

  4. jmeter的使用(一)

    1.下载jmeter:http://jmeter.apache.org/download_jmeter.cgi 2.启动jmeter,打开jmeter.bat 3.添加线程组 4.添加http请求 5 ...

  5. windows下安装和配置mongoDB

    上次在mac下安装和配置了mongodb,这次在windows下也尝试安装和配置mongodb. 1.首先下载mongodb压缩包,下载后解压到D盘或E盘.如下: 2.配置环境变量:桌面—计算机右键— ...

  6. esriSRGeoCS2Type Constants

    ArcGIS Developer Help  (Geometry)     esriSRGeoCS2Type Constants More geographic coordinate systems. ...

  7. StringBuffer与StringBuilder之间的区别

    public class Test { public static void main(String[] args) { StringBuffer strBuffer = new StringBuff ...

  8. Mysql优化方面的知识

    Mysql优化方面的知识 第一方面:30种mysql优化sql语句查询的方法 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避 ...

  9. Scrapy中集成selenium

    面对众多动态网站比如说淘宝等,一般情况下用selenium最好 那么如何集成selenium到scrapy中呢? 因为每一次request的请求都要经过中间件,所以写在中间件中最为合适 from se ...

  10. Ceph的BlueStore总体介绍

    整体架构 bluestore的诞生是为了解决filestore自身维护一套journal并同时还需要基于系统文件系统的写放大问题,并且filestore本身没有对SSD进行优化,因此bluestore ...