CF2205A.Simons and Making It Beautiful
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Those cling-on troubles that ride my stride — cough up the fee, or step aside.
— SHUN, TAKE IT AWAY
For a permutation∗ r of length m, we call an index i (1≤i≤m) ugly if and only if i=max(r1,r2,…,ri).
Simons has a permutation p of length n, and he can perform the following operation on p at most once:
- Choose two indices i and j (1≤i=j≤n), then swap pi and pj.
Find a permutation q that can be obtained from p by performing the above operation at most once, such that the number of ugly indices in q is minimized.
∗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] is not a permutation (2 appears twice in the array), and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
那些如影随形的烦扰——交出费用,抑或退避三舍。
— SHUN,《TAKE IT AWAY》(点击收听)
对于一个长度为 m 的排列∗ r,我们称下标 i(1≤i≤m)是“丑陋的”(ugly),当且仅当 i=max(r1,r2,…,ri)。
西蒙斯拥有一个长度为 n 的排列 p,他最多可对 p 执行如下一次操作:
- 选择两个下标 i 和 j(1≤i=j≤n),然后交换 pi 与 pj。
请找出一个排列 q,它可通过至多执行上述一次操作由 p 得到,使得 q 中丑陋下标的个数最小。
∗ 长度为 n 的排列是指由 1 到 n 这 n 个互不相同的整数组成、顺序任意的数组。例如,[2,3,1,5,4] 是一个排列,而 [1,2,2] 不是排列(数字 2 在数组中出现了两次),[1,3,4] 也不是排列(此时 n=3,但数组中却出现了 4)。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤100). The description of the test cases follows.
The first line contains an integer n (1≤n≤500) — the length of p.
The second line contains n integers p1,p2,…,pn (1≤pi≤n, all pi-s are distinct) — the elements of p.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤100)。随后是各测试用例的描述。
第一行包含一个整数 n(1≤n≤500)—— 表示排列 p 的长度。
第二行包含 n 个整数 p1,p2,…,pn(1≤pi≤n,且所有 pi 互不相同)—— 表示排列 p 的元素。
输出格式
For each test case, print n integers q1,q2,…,qn — the permutation you found.
If there are multiple possible permutations, you may output any.
对于每个测试用例,输出 n 个整数 q1,q2,…,qn —— 即你找到的排列。
如果存在多个可能的排列,你可以输出其中任意一个。
输入输出样例
输入#1
5 2 1 2 4 2 3 1 4 5 3 2 4 5 1 1 1 8 4 1 3 2 6 7 8 5
输出#1
2 1 2 4 1 3 3 2 4 5 1 1 4 1 3 8 6 7 2 5
说明/提示
In the first test case, Simons can obtain only two possible permutations: [1,2] and [2,1].
- For permutation [1,2], we can see 1=max(1) and 2=max(1,2). So there are two ugly indices.
- For permutation [2,1], we can see 1=max(2) and 2=max(2,1). So there is only one ugly index.
Thus, permutation [2,1] has the minimum count of ugly indices.
In the second test case, Simons can obtain permutation [2,4,1,3] by choosing indices 2 and 4 to swap. There is only one ugly index in the permutation:
- 1=max(2), so index 1 is not ugly;
- 2=max(2,4), so index 2 is not ugly;
- 3=max(2,4,1), so index 3 is not ugly;
- 4=max(2,4,1,3), so index 4 is ugly.
In the third test case, note that Simons does not perform the operation.
在第一个测试用例中,西蒙斯只能得到两种可能的排列:[1,2] 和 [2,1]。
- 对于排列 [1,2],我们有 1=max(1) 且 2=max(1,2)。因此共有两个丑陋下标。
- 对于排列 [2,1],我们有 1=max(2) 且 2=max(2,1)。因此仅有一个丑陋下标。
因此,排列 [2,1] 拥有最少的丑陋下标数量。
在第二个测试用例中,西蒙斯可通过选择下标 2 和 4 进行交换,得到排列 [2,4,1,3]。该排列中仅有一个丑陋下标:
- 1=max(2),因此下标 1 不是丑陋下标;
- 2=max(2,4),因此下标 2 不是丑陋下标;
- 3=max(2,4,1),因此下标 3 不是丑陋下标;
- 4=max(2,4,1,3),因此下标 4 是丑陋下标。
在第三个测试用例中,请注意西蒙斯不执行任何操作。
输入解题思路,AI测评打分。不知道怎么写?