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"的更多相关文章

  1. 【Spring】只想用一篇文章记录@Value的使用,不想再找其它了(附思维导图)

    1 简介 不得不说,Spring为大家提供许多开箱即用的功能,@Value就是一个极其常用的功能,它能将配置信息注入到bean中去.即使是一个简单的功能,Spring也提供了丰富的注入类型和形式.我经 ...

  2. HackerRank "Array and simple queries" !

    The most interesting, flexible and juicy binary tree problem I have ever seen. I learnt it from here ...

  3. 【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 ...

  4. list、set、map、array间的相互转换

    list.set.map.array间的相互转换 list转set Set set = new HashSet(new ArrayList()); set转list List list = new A ...

  5. HackerRank "Playing with numbers"

    This is 'Difficult' - I worked out it within 45mins, and unlocked HackerRank Algorithm Level 80 yeah ...

  6. List与Array之间互换

    1 数组转换为List 调用Arrays类的静态方法asList. asList public static <T> List<T> asList(T... a) Return ...

  7. 【HackerRank】Running Time of Quicksort

    题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...

  8. 【Java必修课】一图说尽排序,一文细说Sorting(Array、List、Stream的排序)

    简说排序 排序是极其常见的使用场景,因为在生活中就有很多这样的实例.国家GDP排名.奥运奖牌排名.明星粉丝排名等,各大排行榜,给人的既是动力,也是压力. 而讲到排序,就会有各种排序算法和相关实现,本文 ...

  9. 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 ...

随机推荐

  1. Educational Codeforces Round 14 E.Xor-sequences

    题目链接 分析:K很大,以我现有的极弱的知识储备,大概应该是快速幂了...怎么考虑这个快速幂呢,用到了dp的思想.定义表示从到的合法路径数.那么递推式就是.每次进行这样一次计算,那么序列的长度就会增加 ...

  2. public,protected,private辨析

    一直没有很清楚理解这三个修饰权限的区别,今天终于搞明白了,现总结如下: private:最严格的一个,子类无法继承,只有本类内部内访问,在其余类及子类中通过 "类名.方法" 去调用 ...

  3. R语言apply函数族笔记

    为什么用apply 因为我是一个程序员,所以在最初学习R的时候,当成“又一门编程语言”来学习,但是怎么学都觉得别扭.现在我的看法倾向于,R不是一种通用型的编程语言,而是一种统计领域的软件工具.因此,不 ...

  4. apache认证、授权、访问控制

    认证对象:某一个网站目录. 启用认证 1.即用AllowOverride指令指定哪些指令在针对单个目录的配置文件中有效:AllowOverride AuthConfig 2.设置密码登录访问某个站点或 ...

  5. 如何使用 WinInet 时提供下载上载进度信息

    概要许多开发人员都使用 WinInet 函数来下载或上载文件在 Internet 上的想要提供一个进度条以指示多少文件传输已完成,但多少就越长.您可以使用以下机制来完成此.Collapse image ...

  6. http 网络请求

    /**         * 用 HttpClient 的 Get 请求访问服务器         *          * @param url_path         * @param userN ...

  7. 3、通过挂在系统光盘搭建本地yum仓库的方法

    1. mkdir xxx #新建文件夹 (新建一个挂载需要的文件夹) .配置本地yum源(挂载光盘) .进入 yum.repos.d .ls (查看当前文件夹全部的文件) 并 mv 修改   除Med ...

  8. cassandra CQL 常用操作

    1. CQL客户端链接 bin/cqlsh ip username password 2. (1)建立keyspace语句,keyspace类似于 mysql 中的数据库,一个数据库中可以有很多表: ...

  9. 【转载】【Windows批处理IV】批量进行文件重命名

    1.过滤文件名中所有数字.汉字.特殊字符(含空格) @echo off for %%a in (*.*) do ( if "%%~nxa" neq "%~nx0" ...

  10. 公测后,微信小程序应用可能被拒原因.

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 34.0px "PingFang SC Semibold"; color: #23232 ...