CF1833D.Flipper
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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≤l≤r≤n, a segment is a continuous sequence of numbers pl,pl+1,…,pr−1,pr) and reverse it. Reversing a segment means swapping pairs of numbers (pl,pr), (pl+1,pr−1), ..., (pl+i,pr−i) (where l+i≤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,3,1,5,4, if you choose the segment [l=2,r=3], after reversing the segment p=2,1,3,5,4, then you swap the segments [4,5] and [1,1]. Thus, p=5,4,1,3,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≤i≤n) such that aj=bj for 1≤j<i and ai>bi.
给你一个长度为 n 的排列 p。
排列是由 1 到 n 中互不相同的 n 个整数以任意顺序组成的数组。例如,2,3,1,5,4 是一个排列,而 1,2,2 不是(因为 2 出现了两次),1,3,4 也不是排列(因为 n=3,但数组中包含了 4)。
你需要对排列 p 恰好执行一次如下操作:
- 首先,你选择一个区间 [l,r](其中 1≤l≤r≤n;该区间表示连续的子序列 pl,pl+1,…,pr−1,pr),并将其翻转。翻转区间意味着交换数对 (pl,pr)、(pl+1,pr−1)、……、(pl+i,pr−i)(其中满足 l+i≤r−i)。
- 然后,你交换前缀与后缀:即区间 [r+1,n] 与 [1,l−1](注意:这两个区间可能为空)。
例如,给定 n=5,p=2,3,1,5,4,若你选择区间 [l=2,r=3],则翻转该区间后得到 p=2,1,3,5,4;接着交换区间 [4,5] 与 [1,1],最终得到 p=5,4,1,3,2。可以证明,这是对给定排列所能得到的最大结果。
你需要输出通过恰好执行一次上述操作所能得到的字典序最大的排列。
排列 a 字典序大于排列 b,当且仅当存在某个下标 i(1≤i≤n),使得对所有 1≤j<i 都有 aj=bj,且 ai>bi。
输入格式
The first line of the input contains a single integer t (1≤t≤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≤n≤2000) — the size of the permutation.
The second line of each test case contains n integers: p1,p2,…,pn (1≤pi≤n) — the permutation p itself.
It is guaranteed that the sum of n over all test cases does not exceed 2000.
输入的第一行包含一个整数 t(1≤t≤1000)——测试用例的数量。
随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤2000)——排列的长度。
每个测试用例的第二行包含 n 个整数:p1,p2,…,pn(1≤pi≤n)——排列 p 本身。
保证所有测试用例的 n 之和不超过 2000。
输出格式
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.
对于每个测试用例,在单独一行中输出通过对排列 p 恰好执行一次题目中描述的操作所能得到的长度为 n 的字典序最大的排列。
输入输出样例
输入#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] 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.
第一个示例在题目描述中已作解释。
第二个示例中,应选择区间 [l=9,r=9]。
第三个示例中,应选择区间 [l=1,r=1]。
第四个示例中,应选择区间 [l=1,r=2]。
第五个示例中,应选择区间 [l=5,r=6]。
第六个示例中,应选择区间 [l=4,r=4]。
第七个示例中,应选择区间 [l=5,r=5]。
输入解题思路,AI测评打分。不知道怎么写?