CF2193B.Reverse a Permutation

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] and [1,3,4][1,3,4] are not permutations.

You are given a permutation pp of length nn. You can perform the following operation exactly once:

  • Choose two integers l,l, rr (1≤l≤r≤n1\le l\le r\le n).
  • Reverse the segment [l,r][l, r] in the permutation pp.

Your task is to output the lexicographically maximum permutation that can be obtained by performing this operation. A permutation aa is lexicographically greater than a permutation bb if for the first position ii where they differ, it holds that ai>bia_i\gt b_i.

长度为 nn 的排列是指由 11 到 nn 中互不相同的 nn 个整数以任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 和 [1,3,4][1,3,4] 不是排列。

给定一个长度为 nn 的排列 pp。你可以恰好执行一次如下操作:

  • 选择两个整数 ll、rr(满足 1≤l≤r≤n1\le l\le r\le n);
  • 将排列 pp 中区间 [l,r][l, r] 内的元素进行翻转(即逆序)。

你的任务是输出通过执行该操作所能得到的字典序最大的排列。若排列 aa 与排列 bb 在第一个不同位置 ii 上满足 ai>bia_i > b_i,则称 aa 的字典序大于 bb。

输入格式

Each test consists of several test cases. The first line contains a single integer tt (1≤t≤1041\le t\le 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains the number nn (1≤n≤2⋅1051\le n\le 2\cdot 10^5).

The second line of each test case contains nn distinct integers p1,p2,...,pnp_1, p_2,...,p_n (1≤pi≤n)(1\le p_i\le n).

It is guaranteed that the sum of the values of nn across all test cases does not exceed 2⋅1052\cdot 10^5.

每个测试包含若干测试用例。第一行包含一个整数 tt(1≤t≤1041\le t\le 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051\le n\le 2\cdot 10^5)。

每个测试用例的第二行包含 nn 个互不相同的整数 p1,p2,…,pnp_1, p_2,\dots,p_n(1≤pi≤n1\le p_i\le n)。

保证所有测试用例中 nn 的总和不超过 2⋅1052\cdot 10^5。

输出格式

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][1, 4]. After reversing, a=[4,1,2,3]a = [4, 1, 2, 3]. For the second test case, the best segment is [2,3][2, 3]. After reversing, a=[3,2,1]a = [3, 2, 1].

对于第一个测试用例,最优的子数组是 [1,4][1, 4]。翻转后,a=[4,1,2,3]a = [4, 1, 2, 3]。对于第二个测试用例,最优的子数组是 [2,3][2, 3]。翻转后,a=[3,2,1]a = [3, 2, 1]。

输入解题思路,AI测评打分。不知道怎么写?

首页