CF2195B.Heapify 1
入门
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a permutation a of length n∗.
You can perform the following operation any number of times (possibly zero):
- Select an index i (1≤i≤2n), and swap ai and a2i.
For example, when a=[1,4,2,3,5], you can swap a2 and a4 to make it [1,3,2,4,5], but you cannot swap a2 and a3.
Please determine if the sequence a can be sorted in increasing order.
∗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).
给你一个长度为 n 的排列 a∗。
你可以执行以下操作任意次(包括零次):
- 选择一个下标 i(满足 1≤i≤2n),并交换 ai 和 a2i。
例如,当 a=[1,4,2,3,5] 时,你可以交换 a2 和 a4,使其变为 [1,3,2,4,5];但你不能交换 a2 和 a3。
请判断序列 a 是否能通过若干次上述操作被排序为升序。
∗ 长度为 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≤104). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤2⋅105).
The second line of each test case contains n distinct integers a1,a2,…,an (1≤ai≤n).
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)。
每个测试用例的第二行包含 n 个互不相同的整数 a1,a2,…,an(1≤ai≤n)。
保证所有测试用例的 n 值之和不超过 2⋅105。
输出格式
If a 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.
如果数组 a 能够被排序为升序,则在单独一行输出 "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, a is [1,4,3,2,5]. You can sort a in increasing order by swapping a2 and a4. Therefore, the answer is "YES".
In the second test case, a is [1,4,2,3,5]. It is impossible to sort a in increasing order. Therefore, the answer is "NO".
在第一个测试用例中,a 为 [1,4,3,2,5]。通过交换 a2 和 a4,可将 a 按升序排列。因此,答案为 "YES"。
在第二个测试用例中,a 为 [1,4,2,3,5]。无法将 a 按升序排列。因此,答案为 "NO"。
输入解题思路,AI测评打分。不知道怎么写?