Flipper (Codeforces 1833D)
Tags: Greedy, brute force

You are given a permutation $$$p$$$ of length $$$n$$$.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in any order. For example, $$$\{2,3,1,5,4\}$$$ is a permutation, while $$$\{1,2,2\}$$$ is not (since $$$2$$$ appears twice), and $$$\{1,3,4\}$$$ is also not a permutation (as $$$n=3$$$, but the array contains $$$4$$$).To the permutation $$$p$$$, you need to apply the following operation exactly once:  First you choose a segment $$$[l, r]$$$ ($$$1 \le l \le r \le n$$$, a segment is a continuous sequence of numbers $$$\{p_l, p_{l+1}, \ldots, p_{r-1}, p_r\}$$$) and reverse it. Reversing a segment means swapping pairs of numbers $$$(p_l, p_r)$$$, $$$(p_{l+1}, p_{r-1})$$$, ..., $$$(p_{l + i}, p_{r - i})$$$ (where $$$l + i \le r - i$$$).  Then you swap the prefix and suffix: $$$[r+1, n]$$$ and $$$[1, l - 1]$$$ (note that these segments may be empty). For example, given $$$n = 5, p = \{2, \color{blue}{3}, \color{blue}{1}, 5, 4\}$$$, if you choose the segment $$$[l = 2, r = 3]$$$, after reversing the segment $$$p = \{\color{green}{2}, \color{blue}{1}, \color{blue}{3}, \color{green}{5}, \color{green}{4}\}$$$, then you swap the segments $$$[4, 5]$$$ and $$$[1, 1]$$$. Thus, $$$p = \{\color{green}{5}, \color{green}{4}, 1, 3, \color{green}{2}\}$$$. It can be shown that this is the maximum possible result for the given permutation.You need to output the lexicographically maximum permutation that can be obtained by applying the operation described exactly once.A permutation $$$a$$$ is lexicographically greater than permutation $$$b$$$ if there exists an $$$i$$$ ($$$1 \le i \le n$$$) such that $$$a_j = b_j$$$ for $$$1 \le j < i$$$ and $$$a_i > b_i$$$.

Input: The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.Then the descriptions of the test cases follow.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the size of the permutation.The second line of each test case contains $$$n$$$ integers: $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$) — the permutation $$$p$$$ itself.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$.

Output: For each test case, output in a separate line the lexicographically maximum permutation of length $$$n$$$ that can be obtained from $$$p$$$ by applying the operation described in the problem exactly once.

Note: The first example is explained in the problem statement.In the second example, the segment $$$[l = 9, r = 9]$$$ should be chosen.In the third example, the segment $$$[l = 1, r = 1]$$$ should be chosen.In the fourth example, the segment $$$[l = 1, r = 2]$$$ should be chosen.In the fifth example, the segment $$$[l = 5, r = 6]$$$ should be chosen.In the sixth example, the segment $$$[l = 4, r = 4]$$$ should be chosen.In the seventh example, the segment $$$[l = 5, r = 5]$$$ should be chosen.

Sample Inputs:
9
5
2 3 1 5 4
9
4 1 6 7 2 8 5 3 9
4
4 3 2 1
2
2 1
6
3 2 4 1 5 6
7
3 2 1 5 7 6 4
10
10 2 5 6 1 9 3 8 4 7
4
4 2 1 3
1
1

Sample Outputs:
5 4 1 3 2 
9 4 1 6 7 2 8 5 3 
3 2 1 4 
1 2 
6 5 3 2 4 1 
7 6 4 5 3 2 1 
9 3 8 4 7 1 10 2 5 6 
3 4 2 1 
1

Solution (Python):
output = []
for _ in range(int(input())):
    num_elems = int(input())
    elements = list(map(int, input().split()))
    resultant_elems = []
    if elements[0] == num_elems:
        target_idx = num_elems - 1
    else:
        target_idx = num_elems
    last_occurrence = 0
    for i in range(num_elems):
        if elements[i] == target_idx:
            last_occurrence = i
        if last_occurrence:
            resultant_elems.append(elements[i])
    if not last_occurrence:
        resultant_elems.append(elements[0])
    if last_occurrence == num_elems - 1:
        last_occurrence -= 1
    else:
        resultant_elems.append(elements[last_occurrence - 1])
        last_occurrence -= 2
    while last_occurrence > 0 and elements[last_occurrence] > elements[0]:
        resultant_elems.append(elements[last_occurrence])
        last_occurrence -= 1
    for i in range(last_occurrence + 1):
        resultant_elems.append(elements[i])
    output.append(resultant_elems)
for lst in output:
    print(*lst)

Solution (Python):
n = int(input())
i = 1
ls = []
lsn = []
while i <= n:
    lsn += [int(input())]
    ls += [list(map(int, input().split()))]
    i += 1

def f(n, l):
    if n == 1:
        return [1]
    ls = []
    i = 0
    while i < n:
        if l[i] == n:
            break
        i += 1
    pos = i
    if pos != 0 and pos != n - 1:
        i = pos - 2
        if i == -1:
            i = 0
        while l[i] > l[0] and i > 0:
            i -= 1
        if i == 0:
            t = l[:pos]
            ls = l[pos:] + t[::-1]
            return ls
        else:
            t = l[i + 1:pos]
            ls = l[pos:] + t[::-1] + l[:i + 1]
            return ls
    elif pos == n - 1:
        if l[pos - 1] < l[0]:
            ls = [n] + l[:pos]
            return ls
        else:
            i = pos - 1
            if i == -1:
                i = 0
            while l[i] > l[0] and i > 0:
                i -= 1
            if i == 0:
                ls = l[::-1]
                return ls
            else:
                t = l[i + 1:]
                ls = t[::-1] + l[:i + 1]
                return ls
    else:
        i = 1
        while i < n:
            if l[i] == n - 1:
                break
            i += 1
        poss = i
        if poss == 1:
            ls = l[1:] + [n]
            return ls
        elif poss == n - 1:
            ls = [n - 1] + l[:poss]
            return ls
        else:
            ls = l[poss:] + [l[poss - 1]] + l[:poss - 1]
            return ls

def g(ls):
    s = ''
    i = 0
    while i < len(ls):
        x = ls[i]
        s = s + str(x) + ' '
        i += 1
    return s
i = 0
while i < n:
    print(g(f(lsn[i], ls[i])))
    i += 1

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    if n == 1:
        print(*a)
        continue
    i = a.index(n - 1 if a[0] == n else n)
    if i == n - 1:
        i = n
    j = i - 2
    while j > 0 and a[j] > a[0]:
        j -= 1
    print(*a[i:] + a[j + 1:i][::-1] + a[:j + 1])