CF2193B.Reverse a Permutation
入门
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] and [1,3,4] are not permutations.
You are given a permutation p of length n. You can perform the following operation exactly once:
- Choose two integers l, r (1≤l≤r≤n).
- Reverse the segment [l,r] in the permutation p.
Your task is to output the lexicographically maximum permutation that can be obtained by performing this operation. A permutation a is lexicographically greater than a permutation b if for the first position i where they differ, it holds that ai>bi.
长度为 n 的排列是指由 1 到 n 中互不相同的 n 个整数以任意顺序组成的数组。例如,[2,3,1,5,4] 是一个排列,但 [1,2,2] 和 [1,3,4] 不是排列。
给定一个长度为 n 的排列 p。你可以恰好执行一次如下操作:
- 选择两个整数 l、r(满足 1≤l≤r≤n);
- 将排列 p 中区间 [l,r] 内的元素进行翻转(即逆序)。
你的任务是输出通过执行该操作所能得到的字典序最大的排列。若排列 a 与排列 b 在第一个不同位置 i 上满足 ai>bi,则称 a 的字典序大于 b。
输入格式
Each test consists of several test cases. The first line contains a single integer t (1≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains the number n (1≤n≤2⋅105).
The second line of each test case contains n distinct integers p1,p2,...,pn (1≤pi≤n).
It is guaranteed that the sum of the values of n across all test cases does not exceed 2⋅105.
每个测试包含若干测试用例。第一行包含一个整数 t(1≤t≤104),表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)。
每个测试用例的第二行包含 n 个互不相同的整数 p1,p2,…,pn(1≤pi≤n)。
保证所有测试用例中 n 的总和不超过 2⋅105。
输出格式
For each test case, output the lexicographically maximum permutation that can be obtained with one operation.
对于每个测试用例,输出通过一次操作所能得到的字典序最大的排列。
输入输出样例
输入#1
4 4 3 2 1 4 3 3 1 2 4 4 3 2 1 2 2 1
输出#1
4 1 2 3 3 2 1 4 3 2 1 2 1
说明/提示
For the first test case, the best segment is [1,4]. After reversing, a=[4,1,2,3]. For the second test case, the best segment is [2,3]. After reversing, a=[3,2,1].
对于第一个测试用例,最优的子数组是 [1,4]。翻转后,a=[4,1,2,3]。对于第二个测试用例,最优的子数组是 [2,3]。翻转后,a=[3,2,1]。
输入解题思路,AI测评打分。不知道怎么写?