Codeforces Round #624 (Div. 3) problem B

B. WeirdSorttime

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 11 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

翻译

给您一个长度为n的数组a。还给您一组不同的位置p1,p2,…,pm,其中1≤pi<n。

位置pi表示您可以交换元素api和a [pi + 1]。

您可以为每个给定位置多次应用此操作。

您的任务是确定是否可以仅使用允许的交换以非降序(a1≤a2≤⋯≤an)对初始数组进行排序。

例如,如果a = [3,2,1]和p = [1,2],则我们可以首先交换元素a [2]和a [3](因为位置2包含在给定集合p中)。我们得到数组a = [3,1,2]。然后我们交换a [1]和a [2](位置11也包含在p中)。我们得到数组a = [1,3,2]。

最后,我们再次交换a [2]和a [3],得到数组a = [1,2,3],该数组以非降序排列。

您可以看到,如果a = [4,1,2,3]和p = [3,2],则无法对数组进行排序。

您必须回答t个独立的测试用例。

输入值

输入的第一行包含一个整数t(1≤t≤100)-测试用例的数量。

然后是t个测试用例。

每个测试用例的第一行包含两个整数n和m(1≤m<n≤100),即a中的元素数量和p中的元素数量。

测试用例的第二行包含n个整数a1,a2,…,an(1≤ai≤100)。

测试用例的第三行包含m个整数p1,p2,…,pm(1≤pi<n,所有pi都是不同的)—问题陈述中描述的一组位置。

输出量

对于每个测试用例,如果可以仅使用允许的交换以非降序(a1≤a2≤⋯≤an)对初始数组进行排序,请打印答案—“是”(不带引号)。

否则,打印“否”。

题解
模拟排序过程,使结果按照非递减的顺序排序。


//ac,结构体
#include <bits/stdc++.h>

using namespace std; 

struct AA{
	int s, t, k, v;
};

AA a[110];
int b[110];

bool cmp(AA a, AA b) {
	if(a.v != b.v)
    return a.v < b.v;
    	else return a.s < b.s;
}
int main() {
	int t, m, n;
	scanf("%d", &t);  
	while(t--){    
	scanf("%d%d", &n, &m);       
	for (int i = 1; i <= n; i++) {           
	scanf ("%d", &a[i].v);          
	a[i].s = i;       
	}       
	sort(a + 1, a + 1 + n,cmp);       
	for (int i = 1; i <= n; i ++) {           
	a[i].t = i;           
	a[i].k = 0;        
	}       
	for(int i = 1; i <= m; i++) {           
	scanf("%d", &b[i]);           
	a[b[i]].k = 1;      
	}    
	for (int i = 1; i <= n; i++)            
	int flag = 1;
	for (int i = 1;i <= n; i++) {         
		if(a[i].s < i) {
                	for (int j = a[i].s ; j < a[i].t; j++)
               		 if(!a[j].k) {
                   	 flag = 0;
                    	break;
                	}       
		}         
		if(flag == 0) break;      
	}    
	if(flag) printf("YES\n");     
	else printf("NO\n");
    }
}
//ac,pair
#include <bits/stdc++.h>
 
using namespace std;
 
int p[110];
pair<int, int> a[110];
bool mark[110];
int n, m;
 
void solve() {
    cin >> n >> m;
    memset(mark, false, sizeof(mark));
    for (int i = 0; i < n; i++) {
        cin >> a[i].first;
        a[i].second = i;
    }
    for (int i = 0; i < m; i++) {
        cin >> p[i];
        p[i]--;
        mark[p[i]] = true;
    }
    sort(a, a + n);
    for (int i = 0; i < n; i++) {
        if (a[i].second < i) {//i是排好序的编码,如果原来的编码<i,则模拟冒牌排序
            for (int j = a[i].second; j < i; j++) {
                if (!mark[j]) {
                    cout << "NO\n";
                    return;
                }
            }
        }
    }
    cout << "YES\n";
}
 
int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}
发布了25 篇原创文章 · 获赞 24 · 访问量 592

猜你喜欢

转载自blog.csdn.net/rainbowsea_1/article/details/104492734