91172024-02-14 17:55:14NagyLeoKövetkező permutáció (50 pont)python3Time limit exceeded 10/50342ms12648 KiB
from itertools import permutations


def main():
    n = int(input())
    numbers = list(map(int, input().split()))

    # isAfter = False

    def test(numbers):
        for i in range(len(numbers) - 2):
            for j in range(i + 1, len(numbers) - 1):
                for k in range(j + 1, len(numbers)):
                    if numbers[j] < numbers[k] and numbers[k] < numbers[i]:
                        return False
                        # if numbers[j] < numbers[k]:
                        #    numbers[j], numbers[k] = numbers[k], numbers[j]
        return True

    for i in range(n - 1, 0, -1):
        if numbers[i] > numbers[i - 1]:
            break

    for j in range(n - 1, -1, -1):
        if numbers[j] > numbers[i - 1]:
            break

    numbers[j], numbers[i - 1] = numbers[i - 1], numbers[j]

    # print(numbers,numbers[i:])

    for B in permutations(sorted(numbers[i:])):
        A = numbers[:i] + list(B)
        # print(A)
        if test(A):  # and isAfter:
            print(*A)
            break
        # if A == numbers:
        #    isAfter = True


main()
SubtaskSumTestVerdictTimeMemory
base10/50
1Accepted0/017ms11484 KiB
2Time limit exceeded0/0280ms4628 KiB
3Accepted1/117ms11456 KiB
4Accepted1/117ms11412 KiB
5Accepted1/117ms11776 KiB
6Accepted1/118ms11880 KiB
7Accepted2/218ms12188 KiB
8Time limit exceeded0/2268ms4696 KiB
9Accepted2/225ms12632 KiB
10Accepted2/218ms12648 KiB
11Time limit exceeded0/1300ms5156 KiB
12Time limit exceeded0/1279ms5264 KiB
13Time limit exceeded0/1342ms5240 KiB
14Time limit exceeded0/1254ms5280 KiB
15Time limit exceeded0/3266ms5404 KiB
16Time limit exceeded0/3266ms5840 KiB
17Time limit exceeded0/3263ms5632 KiB
18Time limit exceeded0/3270ms6080 KiB
19Time limit exceeded0/3272ms6656 KiB
20Time limit exceeded0/3275ms6820 KiB
21Time limit exceeded0/3280ms6884 KiB
22Time limit exceeded0/3280ms7192 KiB
23Time limit exceeded0/3282ms7304 KiB
24Time limit exceeded0/3279ms7276 KiB
25Time limit exceeded0/2280ms7844 KiB
26Time limit exceeded0/2270ms7236 KiB