HackerRank "New Year Chaos"
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"的更多相关文章
- [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 ...
- 日常小测:颜色 && Hackerrank Unique_colors
题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...
- HackerRank "Square Subsequences" !!!
Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...
- 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 ...
- HackerRank "TBS Problem" ~ NPC
It is marked as a NPC problem. However from the #1 code submission (https://www.hackerrank.com/Charl ...
- HackerRank Extra long factorials
传送门 今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已. Extra long factorials Authored by vatsalchan ...
- HackerRank "Lucky Numbers"
Great learning for me:https://www.hackerrank.com/rest/contests/master/challenges/lucky-numbers/hacke ...
- HackerRank "Playing with numbers"
This is 'Difficult' - I worked out it within 45mins, and unlocked HackerRank Algorithm Level 80 yeah ...
- HackerRank "The Indian Job"
A sly knapsack problem in disguise! Thanks to https://github.com/bhajunsingh/programming-challanges/ ...
随机推荐
- Laravel Container分析
在分析Laravel流程具体细节之前我们先来了解一下它的Container容器,容器的作用简单的说就是用来存储对象(类名称或者实例),包括提供一些生成对象实例的方法. 我们查看Illuminate\C ...
- [笔记]CSS样式声明顺序
来自Bootstrap中文网编程规范 相关的属性声明应当归为一组,并按照下面的顺序排列: Positioning Box model Typographic Visual .declaration-o ...
- wireshark过滤使用
过滤器的区别 捕捉过滤器(CaptureFilters):用于决定将什么样的信息记录在捕捉结果中.需要在开始捕捉前设置.显示过滤器(DisplayFilters):在捕捉结果中进行详细查找.他们可以在 ...
- VUE应用的一些感受
方便,数据绑定太方便了. 一个组件一个.vue文件特别清晰. 讲真vue比angular好学多了. webpack打包最近看懂,通过一个主文件把require的文件都打进来.业务代码放build里,引 ...
- springmvc自定义日期编辑器
1.控制器 @Controller public class MyController { // 处理器方法 @RequestMapping(value = "/first.do" ...
- i++问题
例题,以下代码的输出结果是什么? #include <stdio.h> int main() { ,,,,}; int *ptr = arr; *(ptr++) += ; printf(& ...
- ADO.NET 增、删、改、查
ADO.NET:数据访问技术 就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中也可以将数据库中的数据提取到内存中供程序调用 所有数据访问技术的基础 连接 ...
- contextloaderlistener
http://blog.csdn.net/c5153000/article/details/6234207 作用:在启动Web容器时,自动装配Spring applicationContext.xml ...
- iOS开发多线程篇—GCD的常见用法
iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...
- Liquid基础语法
Output输出 简单输出示例: Hello {{name}} Hello {{user.name}} Hello {{ 'tobi' }} Advanced output: Filters 高级输出 ...