CF2195B.Heapify 1

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a permutation aa of length nn∗^{\text{∗}}.

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

  • Select an index ii (1≤i≤n21 \le i \le \frac{n}{2}), and swap aia_i and a2ia_{2i}.

For example, when a=[1,4,2,3,5]a=[1,4,2,3,5], you can swap a2a_2 and a4a_4 to make it [1,3,2,4,5][1,3,2,4,5], but you cannot swap a2a_2 and a3a_3.

Please determine if the sequence aa can be sorted in increasing order.

∗^{\text{∗}}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] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

给你一个长度为 nn 的排列 aa∗^{\text{∗}}。

你可以执行以下操作任意次(包括零次):

  • 选择一个下标 ii(满足 1≤i≤n21 \le i \le \frac{n}{2}),并交换 aia_i 和 a2ia_{2i}。

例如,当 a=[1,4,2,3,5]a=[1,4,2,3,5] 时,你可以交换 a2a_2 和 a4a_4,使其变为 [1,3,2,4,5][1,3,2,4,5];但你不能交换 a2a_2 和 a3a_3。

请判断序列 aa 是否能通过若干次上述操作被排序为升序。

∗^{\text{∗}} 长度为 nn 的排列是指由 11 到 nn 中互不相同的 nn 个整数以任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,而 [1,2,2][1,2,2] 不是排列(数字 22 在数组中出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中出现了 44)。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

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

The second line of each test case contains nn distinct integers a1,a2,…,ana_1,a_2,\ldots,a_n (1≤ai≤n1 \le a_i \le n).

It is guaranteed that the sum of nn over 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 个互不相同的整数 a1,a2,…,ana_1,a_2,\ldots,a_n(1≤ai≤n1 \le a_i \le n)。

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

输出格式

If aa can be sorted in increasing order, output "YES" on a separate line. Otherwise, output "NO" on a separate line.

You can output the answer in any case. For example, the strings "yEs", "yes", and "Yes" will also be recognized as positive responses.

如果数组 aa 能够被排序为升序,则在单独一行输出 "YES";否则,在单独一行输出 "NO"。

你可以以任意大小写形式输出答案。例如,字符串 "yEs"、"yes" 和 "Yes" 也会被识别为肯定回答。

输入输出样例

  • 输入#1

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

    输出#1

    YES
    NO

说明/提示

In the first test case, aa is [1,4,3,2,5][1,4,3,2,5]. You can sort aa in increasing order by swapping a2a_2 and a4a_4. Therefore, the answer is "YES".

In the second test case, aa is [1,4,2,3,5][1,4,2,3,5]. It is impossible to sort aa in increasing order. Therefore, the answer is "NO".

在第一个测试用例中,aa 为 [1,4,3,2,5][1,4,3,2,5]。通过交换 a2a_2 和 a4a_4,可将 aa 按升序排列。因此,答案为 "YES"。

在第二个测试用例中,aa 为 [1,4,2,3,5][1,4,2,3,5]。无法将 aa 按升序排列。因此,答案为 "NO"。

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

首页