HackerRank "Larry's Array"
I caught the sparkle in my mind and got AC1 ! It is a great great experience !
So the basic idea: permute on 3 consecutive items doesn't change the parity of the no. of inversions. Each permutation can only remove 0 or 2 inversions. So we say "YES" when no. of inversion % 2 = = 0. And we use MergeSort to count it in O(nlgn).
ret = 0
def merge(arr1, arr2):
global ret
if not arr1: return arr2
if not arr2: return arr1
for v2 in arr2:
arr1.append(v2)
i = len(arr1) - 1
while(i > 0):
if arr1[i] < arr1[i - 1]:
arr1[i - 1],arr1[i] = arr1[i], arr1[i - 1]
i -= 1
ret += 1
else:
break
return arr1 def mergeSort(arr):
n = len(arr)
if (n < 2): return arr
return merge(mergeSort(arr[:n//2]), mergeSort(arr[n//2:])) ###
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
ret = 0
mergeSort(arr)
print("YES" if ret % 2 == 0 else "NO")
HackerRank "Larry's Array"的更多相关文章
- 【Spring】只想用一篇文章记录@Value的使用,不想再找其它了(附思维导图)
1 简介 不得不说,Spring为大家提供许多开箱即用的功能,@Value就是一个极其常用的功能,它能将配置信息注入到bean中去.即使是一个简单的功能,Spring也提供了丰富的注入类型和形式.我经 ...
- HackerRank "Array and simple queries" !
The most interesting, flexible and juicy binary tree problem I have ever seen. I learnt it from here ...
- 【HackerRank】Sherlock and Array
Watson gives an array A1,A2...AN to Sherlock. Then he asks him to find if there exists an element in ...
- list、set、map、array间的相互转换
list.set.map.array间的相互转换 list转set Set set = new HashSet(new ArrayList()); set转list List list = new A ...
- HackerRank "Playing with numbers"
This is 'Difficult' - I worked out it within 45mins, and unlocked HackerRank Algorithm Level 80 yeah ...
- List与Array之间互换
1 数组转换为List 调用Arrays类的静态方法asList. asList public static <T> List<T> asList(T... a) Return ...
- 【HackerRank】Running Time of Quicksort
题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...
- 【Java必修课】一图说尽排序,一文细说Sorting(Array、List、Stream的排序)
简说排序 排序是极其常见的使用场景,因为在生活中就有很多这样的实例.国家GDP排名.奥运奖牌排名.明星粉丝排名等,各大排行榜,给人的既是动力,也是压力. 而讲到排序,就会有各种排序算法和相关实现,本文 ...
- Minimum number of swaps required to sort an array
https://www.hackerrank.com/challenges/minimum-swaps-2/problem Minimum Swaps II You are given an unor ...
随机推荐
- python中main()函数写法
顶顶大名的Guido van Rossum(Python之父)推荐的main写法: #!/usr/bin/python import sys import getopt class Usage(Exc ...
- C++ 高级语法学习与总结(代码实例)
C++11增加了许多的特性,auto就是一个很明显的例子. 还有就是typedid()获取数据变量的类型 看下面简短的代码: atuo: 很像java中的加强for循环..... //获取一个数据 ...
- 20169212《Linux内核原理与分析》第三周作业
最近,深入的阅读了<Linux内核设计与实现>这本书,以下是碰到的一些问题,在此和大家进行交流学习. 碰到的问题 1.为什么不要在linux内核中使用浮点数(这个问题由于书上讲的不够明白, ...
- Windows Store App 获取文件及文件夹列表
通过使用13.2.1小节给出的方法和属性,不仅可以对用户库中的文件和文件夹进行操作,还可以获取其中所有的文件或者文件夹,比如为了完整地展现整个音乐库,可以获取并列举出音乐库中所有的音乐文件,以便能够在 ...
- SQLSERVER存储过程基本语法
一.定义变量 --简单赋值 declare @a int set @a=5 print @a --使用select语句赋值 declare @user1 nvarchar(50) select @us ...
- JavaScript 数组的创建
数组定义:数组(array)是一种数据类型,它包含或者存储了编码的值,每个编码的值称作该数组的一个元素(element), 每个元素的编码被称作为下标(index). JavaScript一维数组创建 ...
- 微信JS-SDK DEMO页面和示例代码
<?php require_once "jssdk.php"; $jssdk = new JSSDK("yourAppID", "yourApp ...
- thread启动线程
- CSS属性之 -- overflow
overflow可以实现隐藏超出对象内容,同时也有显示与隐藏滚动条的作用,overflow属性有四个值:visible (默认), hidden, scroll, 和auto.同样有两个overflo ...
- Spring中加载多个Properties配置文件
单个配置: <bean id="propertyConfigurer" class="org.springframework.beans.factory.confi ...