CF1674D.A-B-C Sort

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given three arrays aa, bb and cc. Initially, array aa consists of nn elements, arrays bb and cc are empty.

You are performing the following algorithm that consists of two steps:

  • Step 11: while aa is not empty, you take the last element from aa and move it in the middle of array bb. If bb currently has odd length, you can choose: place the element from aa to the left or to the right of the middle element of bb. As a result, aa becomes empty and bb consists of nn elements.
  • Step 22: while bb is not empty, you take the middle element from bb and move it to the end of array cc. If bb currently has even length, you can choose which of two middle elements to take. As a result, bb becomes empty and cc now consists of nn elements.

Refer to the Note section for examples.

Can you make array cc sorted in non-decreasing order?

给你三个数组 aa、bb 和 cc。初始时,数组 aa 包含 nn 个元素,而数组 bb 和 cc 均为空。

你将执行如下包含两个步骤的算法:

  • 步骤 1:当 aa 非空时,你不断取出 aa 的最后一个元素,并将其插入到数组 bb 的中间位置。若当前 bb 的长度为奇数,则你可以选择:将 aa 中取出的元素放在 bb 当前中间元素的左侧或右侧。最终,aa 变为空,而 bb 包含 nn 个元素。
  • 步骤 2:当 bb 非空时,你不断取出 bb 的中间元素,并将其添加到数组 cc 的末尾。若当前 bb 的长度为偶数,则你可以任选两个中间元素中的一个取出。最终,bb 变为空,而 cc 现在包含 nn 个元素。

示例请参见“注释”(Note)部分。

你能否通过合理选择所有可选操作,使得最终得到的数组 cc 是非递减有序的?

输入格式

The first line contains a single integer tt (1≤t≤2⋅1041 \le t \le 2 \cdot 10^4) — the number of test cases. Next tt cases follow.

The first line of each test case contains the single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the length of array aa.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1061 \le a_i \le 10^6) — the array aa itself.

It's guaranteed that the sum of nn doesn't exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤2⋅1041 \le t \le 2 \cdot 10^4)—— 测试用例的数量。接下来是 tt 个测试用例。

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

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1061 \le a_i \le 10^6)—— 数组 aa 本身。

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

输出格式

For each test, print YES (case-insensitive), if you can make array cc sorted in non-decreasing order. Otherwise, print NO (case-insensitive).

对于每个测试用例,如果可以将数组 cc 变为非递减排序,则输出 YES(不区分大小写);否则输出 NO(不区分大小写)。

输入输出样例

  • 输入#1

    3
    4
    3 1 5 3
    3
    3 2 1
    1
    7331

    输出#1

    YES
    NO
    YES

说明/提示

In the first test case, we can do the following for a=[3,1,5,3]a = [3, 1, 5, 3]:

Step 11:

aa

[3,1,5,3][3, 1, 5, 3]

⇒\Rightarrow

[3,1,5][3, 1, 5]

⇒\Rightarrow

[3,1][3, 1]

⇒\Rightarrow

[3][3]

⇒\Rightarrow

[][]

bb

[][]

[3‾][\underline{3}]

[3,5‾][3, \underline{5}]

[3,1‾,5][3, \underline{1}, 5]

[3,3‾,1,5][3, \underline{3}, 1, 5]

Step 22:

bb

[3,3,1‾,5][3, 3, \underline{1}, 5]

⇒\Rightarrow

[3,3‾,5][3, \underline{3}, 5]

⇒\Rightarrow

[3‾,5][\underline{3}, 5]

⇒\Rightarrow

[5‾][\underline{5}]

⇒\Rightarrow

[][]

cc

[][]

[1][1]

[1,3][1, 3]

[1,3,3][1, 3, 3]

[1,3,3,5][1, 3, 3, 5]

As a result, array c=[1,3,3,5]c = [1, 3, 3, 5] and it's sorted.

在第一个测试用例中,对于 a=[3,1,5,3]a = [3, 1, 5, 3],我们可以执行如下操作:

步骤 11:

aa

[3,1,5,3][3, 1, 5, 3]

⇒\Rightarrow

[3,1,5][3, 1, 5]

⇒\Rightarrow

[3,1][3, 1]

⇒\Rightarrow

[3][3]

⇒\Rightarrow

[][]

bb

[][]

[3‾][\underline{3}]

[3,5‾][3, \underline{5}]

[3,1‾,5][3, \underline{1}, 5]

[3,3‾,1,5][3, \underline{3}, 1, 5]

步骤 22:

bb

[3,3,1‾,5][3, 3, \underline{1}, 5]

⇒\Rightarrow

[3,3‾,5][3, \underline{3}, 5]

⇒\Rightarrow

[3‾,5][\underline{3}, 5]

⇒\Rightarrow

[5‾][\underline{5}]

⇒\Rightarrow

[][]

cc

[][]

[1][1]

[1,3][1, 3]

[1,3,3][1, 3, 3]

[1,3,3,5][1, 3, 3, 5]

最终,数组 c=[1,3,3,5]c = [1, 3, 3, 5],且它是有序的。

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

首页