[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 chaotic
Expected OutputDownload
Too chaotic
7


-------
这题有问题吧,4如果要回到3的右边,一定要越过678 肯定超过和两个人交换的限制了,怎么可能是7呢?


不成功的代码:
#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the minimumBribes function below.
def sort(n,q):
    count = 0

    for i in range(n - 1):
        if q[i] < q[i + 1]:
            continue
        else:
            temp = q[i + 1]
            q[i + 1] = q[i]
            q[i] = temp
            count += 1
    return count

def isDoable(n,q):
    orderList =[]
    for index in range(1,n+1):
        orderList.append(index)

    for index in range(n):
        if (abs(q[index] - orderList[index])) > 2 :
            return False

def minimumBribes(n,q):

    if (isDoable(n,q) == False):
        return 'Too chaotic'

    countChange = 0

    while(all([q[i] < q[i + 1] for i in range(len(q) - 1)]) == False):
        countChange += sort(n, q)
    return countChange

if __name__ == '__main__':
    t = int(input())

    for t_itr in range(t):
        n = int(input())

        q = list(map(int, input().rstrip().split()))

        print(minimumBribes(n ,q))

 




posted @ 2019-01-09 18:01  夜歌乘年少  阅读(466)  评论(0编辑  收藏  举报