CF1750A.Indirect Sort

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a permutation a1,a2,…,ana_1, a_2, \ldots, a_n of size nn, where each integer from 11 to nn appears exactly once.

You can do the following operation any number of times (possibly, zero):

  • Choose any three indices i,j,ki, j, k (1≤i<j<k≤n1 \le i \lt j \lt k \le n).
  • If ai>aka_i \gt a_k, replace aia_i with ai+aja_i + a_j. Otherwise, swap aja_j and aka_k.

Determine whether you can make the array aa sorted in non-descending order.

给你一个长度为 nn 的排列 a1,a2,…,ana_1, a_2, \ldots, a_n,其中 11 到 nn 的每个整数恰好出现一次。

你可以执行以下操作任意多次(也可以不执行):

  • 任选三个下标 i,j,ki, j, k(满足 1≤i<j<k≤n1 \le i \lt j \lt k \le n);
  • 若 ai>aka_i \gt a_k,则将 aia_i 替换为 ai+aja_i + a_j;否则,交换 aja_j 和 aka_k。

判断是否能通过若干次上述操作,使数组 aa 变为非降序排列。

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤50001 \le t \le 5000) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer nn (3≤n≤103 \le n \le 10) — the length of the array aa.

The second line contains nn integers a1,a2,…,ana_1,a_2,\dots,a_n (1≤ai≤n1 \le a_i \le n, ai≠aja_i \neq a_j if i≠ji \neq j) — the elements of the array aa.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤50001 \le t \le 5000),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(3≤n≤103 \le n \le 10),表示数组 aa 的长度。

第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\dots,a_n(1≤ai≤n1 \le a_i \le n,且当 i≠ji \neq j 时 ai≠aja_i \neq a_j),表示数组 aa 的元素。

输出格式

For each test case, output "Yes" (without quotes) if the array can be sorted in non-descending order, and "No" (without quotes) otherwise.

You can output "Yes" and "No" in any case (for example, strings "YES", "yEs" and "yes" will be recognized as a positive response).

对于每个测试用例,如果该数组可以被排序为非降序,则输出“Yes”(不带引号);否则输出“No”(不带引号)。

“Yes”和“No”可使用任意大小写形式输出(例如,字符串“YES”、“yEs”和“yes”均会被识别为肯定回答)。

输入输出样例

  • 输入#1

    7
    3
    1 2 3
    3
    1 3 2
    7
    5 3 4 7 6 2 1
    7
    7 6 5 4 3 2 1
    5
    2 1 4 5 3
    5
    2 1 3 4 5
    7
    1 2 6 7 4 3 5

    输出#1

    Yes
    Yes
    No
    No
    No
    No
    Yes

说明/提示

In the first test case, [1,2,3][1,2,3] is already sorted in non-descending order.

In the second test case, we can choose i=1,j=2,k=3i = 1,j = 2,k = 3. Since a1≤a3a_1 \le a_3, swap a2a_2 and a3a_3, the array then becomes [1,2,3][1,2,3], which is sorted in non-descending order.

In the seventh test case, we can do the following operations successively:

  • Choose i=5,j=6,k=7i = 5,j = 6,k = 7. Since a5≤a7a_5 \le a_7, swap a6a_6 and a7a_7, the array then becomes [1,2,6,7,4,5,3][1,2,6,7,4,5,3].
  • Choose i=5,j=6,k=7i = 5,j = 6,k = 7. Since a5>a7a_5 \gt a_7, replace a5a_5 with a5+a6=9a_5+a_6=9, the array then becomes [1,2,6,7,9,5,3][1,2,6,7,9,5,3].
  • Choose i=2,j=5,k=7i = 2,j = 5,k = 7. Since a2≤a7a_2 \le a_7, swap a5a_5 and a7a_7, the array then becomes [1,2,6,7,3,5,9][1,2,6,7,3,5,9].
  • Choose i=2,j=4,k=6i = 2,j = 4,k = 6. Since a2≤a6a_2 \le a_6, swap a4a_4 and a6a_6, the array then becomes [1,2,6,5,3,7,9][1,2,6,5,3,7,9].
  • Choose i=1,j=3,k=5i = 1,j = 3,k = 5. Since a1≤a5a_1 \le a_5, swap a3a_3 and a5a_5, the array then becomes [1,2,3,5,6,7,9][1,2,3,5,6,7,9], which is sorted in non-descending order.

In the third, the fourth, the fifth and the sixth test cases, it can be shown that the array cannot be sorted in non-descending order.

在第一个测试用例中,[1,2,3][1,2,3] 已经按非降序排列。

在第二个测试用例中,我们可以选择 i=1,j=2,k=3i = 1,j = 2,k = 3。由于 a1≤a3a_1 \le a_3,交换 a2a_2 和 a3a_3,数组变为 [1,2,3][1,2,3],即已按非降序排列。

在第七个测试用例中,我们可以依次执行以下操作:

  • 选择 i=5,j=6,k=7i = 5,j = 6,k = 7。由于 a5≤a7a_5 \le a_7,交换 a6a_6 和 a7a_7,数组变为 [1,2,6,7,4,5,3][1,2,6,7,4,5,3]。
  • 选择 i=5,j=6,k=7i = 5,j = 6,k = 7。由于 a5>a7a_5 \gt a_7,将 a5a_5 替换为 a5+a6=9a_5+a_6=9,数组变为 [1,2,6,7,9,5,3][1,2,6,7,9,5,3]。
  • 选择 i=2,j=5,k=7i = 2,j = 5,k = 7。由于 a2≤a7a_2 \le a_7,交换 a5a_5 和 a7a_7,数组变为 [1,2,6,7,3,5,9][1,2,6,7,3,5,9]。
  • 选择 i=2,j=4,k=6i = 2,j = 4,k = 6。由于 a2≤a6a_2 \le a_6,交换 a4a_4 和 a6a_6,数组变为 [1,2,6,5,3,7,9][1,2,6,5,3,7,9]。
  • 选择 i=1,j=3,k=5i = 1,j = 3,k = 5。由于 a1≤a5a_1 \le a_5,交换 a3a_3 和 a5a_5,数组变为 [1,2,3,5,6,7,9][1,2,3,5,6,7,9],即已按非降序排列。

在第三个、第四个、第五个和第六个测试用例中,可以证明该数组无法被排序为非降序排列。

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

首页