CF1311B.WeirdSort

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an array aa of length nn.

You are also given a set of distinct positions p1,p2,…,pmp_1, p_2, \dots, p_m, where 1≤pi<n1 \le p_i \lt n. The position pip_i means that you can swap elements a[pi]a[p_i] and a[pi+1]a[p_i + 1]. You can apply this operation any number of times for each of the given positions.

Your task is to determine if it is possible to sort the initial array in non-decreasing order (a1≤a2≤⋯≤ana_1 \le a_2 \le \dots \le a_n) using only allowed swaps.

For example, if a=[3,2,1]a = [3, 2, 1] and p=[1,2]p = [1, 2], then we can first swap elements a[2]a[2] and a[3]a[3] (because position 22 is contained in the given set pp). We get the array a=[3,1,2]a = [3, 1, 2]. Then we swap a[1]a[1] and a[2]a[2] (position 11 is also contained in pp). We get the array a=[1,3,2]a = [1, 3, 2]. Finally, we swap a[2]a[2] and a[3]a[3] again and get the array a=[1,2,3]a = [1, 2, 3], sorted in non-decreasing order.

You can see that if a=[4,1,2,3]a = [4, 1, 2, 3] and p=[3,2]p = [3, 2] then you cannot sort the array.

You have to answer tt independent test cases.

给你一个长度为 nn 的数组 aa。

你还给定一组互不相同的下标 p1,p2,…,pmp_1, p_2, \dots, p_m,其中 1≤pi<n1 \le p_i \lt n。下标 pip_i 表示你可以交换元素 a[pi]a[p_i] 和 a[pi+1]a[p_i + 1]。对于每个给定的下标,该交换操作可以执行任意多次。

你的任务是判断:仅使用允许的交换操作,能否将初始数组排序为非递减顺序(即满足 a1≤a2≤⋯≤ana_1 \le a_2 \le \dots \le a_n)?

例如,若 a=[3,2,1]a = [3, 2, 1] 且 p=[1,2]p = [1, 2],则我们可以首先交换 a[2]a[2] 和 a[3]a[3](因为下标 22 属于给定集合 pp),得到数组 a=[3,1,2]a = [3, 1, 2];接着交换 a[1]a[1] 和 a[2]a[2](下标 11 也在 pp 中),得到 a=[1,3,2]a = [1, 3, 2];最后再次交换 a[2]a[2] 和 a[3]a[3],得到 a=[1,2,3]a = [1, 2, 3],即已按非递减顺序排好序。

你还可以观察到:若 a=[4,1,2,3]a = [4, 1, 2, 3] 且 p=[3,2]p = [3, 2],则无法将该数组排序。

你需要回答 tt 个相互独立的测试用例。

输入格式

The first line of the input contains one integer tt (1≤t≤1001 \le t \le 100) — the number of test cases.

Then tt test cases follow. The first line of each test case contains two integers nn and mm (1≤m<n≤1001 \le m \lt n \le 100) — the number of elements in aa and the number of elements in pp. The second line of the test case contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1001 \le a_i \le 100). The third line of the test case contains mm integers p1,p2,…,pmp_1, p_2, \dots, p_m (1≤pi<n1 \le p_i \lt n, all pip_i are distinct) — the set of positions described in the problem statement.

输入的第一行包含一个整数 tt(1≤t≤1001 \le t \le 100),表示测试用例的数量。

接下来是 tt 个测试用例。每个测试用例的第一行包含两个整数 nn 和 mm(1≤m<n≤1001 \le m \lt n \le 100),分别表示数组 aa 的元素个数和数组 pp 的元素个数。每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1001 \le a_i \le 100)。每个测试用例的第三行包含 mm 个整数 p1,p2,…,pmp_1, p_2, \dots, p_m(1≤pi<n1 \le p_i \lt n,且所有 pip_i 互不相同),即题目描述中所指的位置集合。

输出格式

For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order (a1≤a2≤⋯≤ana_1 \le a_2 \le \dots \le a_n) using only allowed swaps. Otherwise, print "NO".

对于每个测试用例,输出答案——如果仅使用允许的交换操作就能将初始数组排序为非递减顺序(即 a1≤a2≤⋯≤ana_1 \le a_2 \le \dots \le a_n),则输出 "YES"(不带引号);否则输出 "NO"。

输入输出样例

  • 输入#1

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

    输出#1

    YES
    NO
    YES
    YES
    NO
    YES

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

首页