Codeforces Round #624 (Div. 3) B

链接:http://codeforces.com/problemset/problem/1311/B
题意:
给一个序列 a 和一个序列 p, 规定在序列 a 中只有 p[i] 和 p[i]+1的位置可以交换位置,不限交换的次数,问最后a是否可以变成有序序列,可以输出"YES",否则输出"NO".
思路:
冒泡排序的变形,不必每一个都尝试冒泡交换,只需要在 p[i] 和 p[i] + 1 处交换,最后再p判断一下 a 是否是有序的即可。

/*****************************
*author:ccf
*source:cf_round_624_B 
*topic:
*******************************/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#define ll long long
using namespace std;

const int N = 105;
int n,m,cas,k;
int a[N],p[N];
int main(){
	freopen("data.in","r",stdin);
	scanf("%d",&cas);
	while(cas--){
		scanf("%d %d",&n,&m);
		memset(bk,false,sizeof bk);
		for(int i = 1; i <= n; ++i){
			scanf("%d",a+i);
		}
		for(int i = 1; i <= m; ++i) scanf("%d",p+i);
		for(int j = 1; j < n; ++j){
			for(int i  = 1; i <= m; ++i)
				if(a[p[i]] > a[p[i]+1]) swap(a[p[i]],a[p[i]+1]);
		}

		if(is_sorted(a+1,a+n+1)) printf("YES\n");
		else printf("NO\n");
	} 
	return 0;
}

发布了141 篇原创文章 · 获赞 71 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/sinat_40872274/article/details/104874182