B. WeirdSort
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an arrayaof lengthn.

You are also given a set ofdistinctpositionsp1,p2,…,pm, where1≤pi<n.The positionpimeans that you can swap elementsa[pi]anda[pi+1].You can apply this operation any number of times for each of the givenpositions.

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, ifa=[3,2,1]andp=[1,2], then we can first swap elementsa[2]anda[3](because position2is contained in the given setp).We get the arraya=[3,1,2].Then we swapa[1]anda[2](position1is also contained inp).We get the arraya=[1,3,2].Finally, we swapa[2]anda[3]again and get the arraya=[1,2,3], sorted in non-decreasing order.

You can see that ifa=[4,1,2,3]andp=[3,2]then you cannot sort the array.

You have to answertindependent test cases.

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

Thenttest cases follow.The first line of each test case contains two integersnandm (1≤m<n≤100) — the number of elements inaand the number of elements inp.The second line of the test case containsnintegersa1,a2,…,an (1≤ai≤100).The third line of the test case containsmintegersp1,p2,…,pm (1≤pi<n, allpiare 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
inputCopy
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
outputCopy
YES
NO
YES
YES
NO
YES

``````#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define me(a,x) memset(a,x,sizeof a)
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define all(x) (x).begin(), (x).end()
#define pb(a) push_back(a)
#define paii pair<int,int>
#define pali pair<ll,int>
#define pail pair<int,ll>
#define pall pair<ll,ll>
#define fi first
#define se second
int a[105];
int p[105];
int main()
{
int t;
cin>>t;
while(t--)
{
me(p,0);
int n,m;
cin>>n>>m;
for(int i=1; i<=n; i++)
cin>>a[i];
for(int i=1; i<=m; i++)
{
int x;
cin>>x;
p[x]=1;
}
int flag=0;
for(int i=1; i<=n; i++)
{
for(int j=n; j; j--)
{
if(p[j-1] && a[j-1]>a[j])
{
int t=a[j-1];
a[j-1]=a[j];
a[j]=t;
}
else if(a[j]>=a[j-1])
continue;
else
{
flag=1;
break;
}
}
}
if(flag)
puts("NO");
else
puts("YES");

}
return 0;
}

``````