Two tricks here:

1. Counting no. of inversed pairs - using Merge Sort, nothing special

2. How to check 'chaotic'? We simply check if any number is over 2 slots away from its own slot : )

#!/bin/python3

import sys

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().strip())
for _ in range(T):
ret = 0
n = int(input().strip())
q = [int(q_temp) for q_temp in input().strip().split(' ')]
# your code goes here
bTooCh = False
for i in range(n):
if q[i] - i > 3:
bTooCh = True
break
if bTooCh:
print ("Too chaotic")
continue mergeSort(q)
print (ret)

HackerRank "New Year Chaos"的更多相关文章

  1. [HackerRank]New Year Chaos[UNDONE]

    Input (stdin)Download 2 8 5 1 2 3 7 8 6 4 8 1 2 5 3 7 8 6 4 Your Output (stdout) Too chaotic Too cha ...

  2. 日常小测:颜色 && Hackerrank Unique_colors

    题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...

  3. HackerRank "Square Subsequences" !!!

    Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...

  4. HackerRank "Minimum Penalty Path"

    It is about how to choose btw. BFS and DFS. My init thought was to DFS - TLE\MLE. And its editorial ...

  5. HackerRank "TBS Problem" ~ NPC

    It is marked as a NPC problem. However from the #1 code submission (https://www.hackerrank.com/Charl ...

  6. HackerRank Extra long factorials

    传送门 今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已. Extra long factorials Authored by vatsalchan ...

  7. HackerRank "Lucky Numbers"

    Great learning for me:https://www.hackerrank.com/rest/contests/master/challenges/lucky-numbers/hacke ...

  8. HackerRank "Playing with numbers"

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

  9. HackerRank "The Indian Job"

    A sly knapsack problem in disguise! Thanks to https://github.com/bhajunsingh/programming-challanges/ ...

随机推荐

  1. Windows C++ 子目录数量

    CFileFind OneFile; BOOL bWorking = FALSE; ; ) != "\\") { strPath += "\\*.*"; } b ...

  2. (转)js activexobject调用客户机exe文件

    原文地址:http://blog.csdn.net/jiafugui/article/details/5364210 function Run(strPath) { try { var objShel ...

  3. 0525Scrum项目7.0

    一.Spring1回顾: 在这第一个冲刺中,我们主要是完成了主界面的建立,过程虽然有些曲折,大家有时候找的素材.图片都有一些冲突,但是最后我们还是求同存异,努力地做好界面! 在这一个冲刺中,我们虽然算 ...

  4. UIImageWriteToSavedPhotosAlbum

    UIImageWriteToSavedPhotosAlbum: Next UIKit Function Reference Overview The UIKit framework defines a ...

  5. launch文件

    launch在ROS应用中,每个节点通常有许多参数需要设置,为了方便高效操作多个节点,可以编写launch文件,然后用roslaunch命令运行roslaunch: roslaunch [option ...

  6. SharePoint\O365 "See also"功能 and site feature 激活\禁用小节

    博客地址:http://blog.csdn.net/FoxDave 最近因为问题发现了SharePoint的"See also"功能,该功能是由SharePoint自动的Feat ...

  7. javascript之DOM篇二(操作)

    一.创建DOM元素 createElement:document.createElement(' 所要创建的元素标签名'): <!DOCTYPE html><html>< ...

  8. C++ UFunction({FLAG}) 宏 FLAG 解释笔记

    1.BluePrintCallable  --蓝图可调用 但不可编辑 2.BlueprintImplementableEvent --函数体必须实现与Blueprint 但函数名必须生成与C++ .h ...

  9. USACO 08-Nov( 最小生成树)

    美国人出题拐弯抹角,倒是挺尊重动物的 10206301 2 52 3 52 4 123 4 172 5 153 5 64 5 12 Hint从牧场4起床, 然后按照 4, 5, 4, 2, 3, 2, ...

  10. HDU1215(筛选法)

    题意:求n的所有因子和: 思路:类似于筛选法求素数的思想,只有第一次的时候了解过它的思想,然后就只是用来求素数,思想的运用反而少: 筛选法求素数: int prime() { memset(vis, ...