CF1833D.Flipper

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a permutation pp of length nn .

A permutation is an array consisting of nn distinct integers from 11 to nn in any order. For example, {2,3,1,5,4}\{2,3,1,5,4\} is a permutation, while {1,2,2}\{1,2,2\} is not (since 22 appears twice), and {1,3,4}\{1,3,4\} is also not a permutation (as n=3n=3 , but the array contains 44 ).

To the permutation pp , you need to apply the following operation exactly once:

  • First you choose a segment [l,r][l, r] ( 1lrn1 \le l \le r \le n , a segment is a continuous sequence of numbers {pl,pl+1,,pr1,pr}\{p_l, p_{l+1}, \ldots, p_{r-1}, p_r\} ) and reverse it. Reversing a segment means swapping pairs of numbers (pl,pr)(p_l, p_r) , (pl+1,pr1)(p_{l+1}, p_{r-1}) , ..., (pl+i,pri)(p_{l + i}, p_{r - i}) (where l+iril + i \le r - i ).
  • Then you swap the prefix and suffix: [r+1,n][r+1, n] and [1,l1][1, l - 1] (note that these segments may be empty).

For example, given n=5,p={2,3,1,5,4}n = 5, p = \{2, \color{blue}{3}, \color{blue}{1}, 5, 4\} , if you choose the segment [l=2,r=3][l = 2, r = 3] , after reversing the segment p={2,1,3,5,4}p = \{\color{green}{2}, \color{blue}{1}, \color{blue}{3}, \color{green}{5}, \color{green}{4}\} , then you swap the segments [4,5][4, 5] and [1,1][1, 1] . Thus, p={5,4,1,3,2}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 aa is lexicographically greater than permutation bb if there exists an ii ( 1in1 \le i \le n ) such that aj=bja_j = b_j for 1j<i1 \le j < i and ai>bia_i > b_i .

输入格式

The first line of the input contains a single integer tt ( 1t10001 \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 nn ( 1n20001 \le n \le 2000 ) — the size of the permutation.

The second line of each test case contains nn integers: p1,p2,,pnp_1, p_2, \ldots, p_n ( 1pin1 \le p_i \le n ) — the permutation pp itself.

It is guaranteed that the sum of nn over all test cases does not exceed 20002000 .

输出格式

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

输入输出样例

  • 输入#1

    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

    输出#1

    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

说明/提示

The first example is explained in the problem statement.

In the second example, the segment [l=9,r=9][l = 9, r = 9] should be chosen.

In the third example, the segment [l=1,r=1][l = 1, r = 1] should be chosen.

In the fourth example, the segment [l=1,r=2][l = 1, r = 2] should be chosen.

In the fifth example, the segment [l=5,r=6][l = 5, r = 6] should be chosen.

In the sixth example, the segment [l=4,r=4][l = 4, r = 4] should be chosen.

In the seventh example, the segment [l=5,r=5][l = 5, r = 5] should be chosen.

首页