WeirdSort

You are given an array a of length n.

You are also given a set of distinct positions p1,p2,…,pm, where 1≤pi<n. The position pi means that you can swap elements a[pi] and a[pi+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≤⋯≤an) using only allowed swaps.

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

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

You have to answer t independent test cases.

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

Then t test cases follow. The first line of each test case contains two integers n and m (1≤m<n≤100) — the number of elements in a and the number of elements in p. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤100). The third line of the test case contains m integers p1,p2,…,pm (1≤pi<n, all pi are distinct) — the set of positions described in the problem statement.

Output
For each test case, print the answer — “YES” (without quotes) if you can sort the initial array in non-decreasing order (a1≤a2≤⋯≤an) using only allowed swaps. Otherwise, print “NO”.

Example

input
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
output
YES
NO
YES
YES
NO
YES

写出一个很sb的bug,贴出来长记性。
思路:读取一个下标p,就将st[p]和st[p+1]++.
将a数组中的数据放在b数组中排序,遍历st数组,
如果st[i]==0,说明在排序过程中a[i]不能被交换,判断a[i]是否等于b[i]
如果st[i]不为0,则会有一段连续的序列,头和尾是1,中间可能会有2,找到这段序列,
显然这段序列中的元素是可以任意交换位置的,检查a[i],和b[i]在这段区域中的元素是否种类和数量相等.

#include<bits/stdc++.h>

using namespace std;
const int N = 105;
int t, a[N], b[N], n, m;
int st[N], cnta[N], cntb[N];

int main() {
    cin >> t;
    while (t--) {
        memset(st, 0, sizeof(st));
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++)scanf("%d", &a[i]), b[i] = a[i];
        for (int i = 1; i <= m; i++) {
            int p;
            scanf("%d", &p);
            st[p]++, st[p + 1]++;
        }
        sort(b + 1, b + 1 + n);
        bool flag = true;
        for (int i = 1; i <= n;) {
            if (st[i] == 0) {
                if (a[i] != b[i]) {
                    flag = false;
                    break;
                }
                i++;
            } else {
                int sta = i;
                memset(cnta, 0, sizeof(cnta));
                ++cnta[a[i++]];
                while (st[i] == 2)++cnta[a[i++]];
                ++cnta[a[i++]];
                memset(cntb, 0, sizeof(cntb));
                for (int j = sta; j < i; j++)cntb[b[j]]++;
                for (int j = 1; j <= 100; j++)
                    if (cnta[j] != cntb[j]) {
                        flag = false;
                        break;
                    }
            }
            if (!flag)break;
        }
        puts(flag ? "YES" : "NO");
    }
    return 0;
}
发布了360 篇原创文章 · 获赞 28 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_45323960/article/details/105128289