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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章