Codeforces Round #624 (Div. 3) B. WeirdSort(排序)
standard output
You are given an array aa of length nn .
You are also given a set of distinct positions p1,p2,…,pmp1,p2,…,pm , where 1≤pi<n1≤pi<n . The position pipi means that you can swap elements a[pi]a[pi] and a[pi+1]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≤⋯≤ana1≤a2≤⋯≤an ) using only allowed swaps.
For example, if a=[3,2,1]a=[3,2,1] and p=[1,2]p=[1,2] , then we can first swap elements a[2]a[2] and a[3]a[3] (because position 22 is contained in the given set pp ). We get the array a=[3,1,2]a=[3,1,2] . Then we swap a[1]a[1] and a[2]a[2] (position 11 is also contained in pp ). We get the array a=[1,3,2]a=[1,3,2] . Finally, we swap a[2]a[2] and a[3]a[3] again and get the array a=[1,2,3]a=[1,2,3] , sorted in non-decreasing order.
You can see that if a=[4,1,2,3]a=[4,1,2,3] and p=[3,2]p=[3,2] then you cannot sort the array.
You have to answer tt independent test cases.
The first line of the input contains one integer tt (1≤t≤1001≤t≤100 ) — the number of test cases.
Then tt test cases follow. The first line of each test case contains two integers nn and mm (1≤m<n≤1001≤m<n≤100 ) — the number of elements in aa and the number of elements in pp . The second line of the test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1001≤ai≤100 ). The third line of the test case contains mm integers p1,p2,…,pmp1,p2,…,pm (1≤pi<n1≤pi<n , all pipi are distinct) — the set of positions described in the problem statement.
For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order (a1≤a2≤⋯≤ana1≤a2≤⋯≤an ) using only allowed swaps. Otherwise, print "NO".
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
YES
NO
YES
YES
NO
YES 大意是给定序列a和p,通过一定次数的交换操作(只能swap(a[p[i]],a[p[i]+1]))能否让a序列单调不减。可以这么想,因为每次只能交换相邻两个数,所以当一个数要到达最终排序好的位置的话,肯定是一步一步挪过去的,自然想到冒泡,根据题意这里选择从前往后把大的数交换到后面,每次需要swap的时候,先检查下标是否在p数组里。理论上可以sort一遍p数组然后二分,但是看到数据量这么小直接O(n)检查也能过。
#include <bits/stdc++.h>
using namespace std;
int a[],p[];
int n,m;
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
int i,j,k;
bool flag=;
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=;i<=m;i++)
{
scanf("%d",&p[i]);
}
for(i=;i<=n-;i++)
{
for(j=;j<=n-i;j++)
{
if(a[j]>a[j+])
{
int find=;
for(k=;k<=m;k++)
{
if(p[k]==j)
{
int t=a[j];
a[j]=a[j+];
a[j+]=t;
find=;
break;
}
}
if(!find)
{
flag=;
goto label;
}
}
}
}
label:;
if(!flag)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
} }
return ;
}
Codeforces Round #624 (Div. 3) B. WeirdSort(排序)的更多相关文章
- Codeforces Round #624 (Div. 3)(题解)
Codeforces Round #624 (Div.3) 题目地址:https://codeforces.ml/contest/1311 B题:WeirdSort 题意:给出含有n个元素的数组a,和 ...
- Codeforces Round #624 (Div. 3) F. Moving Points 题解
第一次写博客 ,请多指教! 翻了翻前面的题解发现都是用树状数组来做,这里更新一个 线段树+离散化的做法: 其实这道题是没有必要用线段树的,树状数组就能够解决.但是个人感觉把线段树用熟了会比树状数组更有 ...
- Codeforces Round #624 (Div. 3)(题解)
A. Add Odd or Subtract Even 思路: 相同直接为0,如果两数相差为偶数就为2,奇数就为1 #include<iostream> #include<algor ...
- 详细讲解Codeforces Round #624 (Div. 3) F. Moving Points
题意:给定n个点的初始坐标x和速度v(保证n个点的初始坐标互不相同), d(i,j)是第i个和第j个点之间任意某个时刻的最小距离,求出n个点中任意一对点的d(i,j)的总和. 题解:可以理解,两个点中 ...
- Codeforces Round #624 (Div. 3)
A.题意:通过加奇数减偶数的操作从a到b最少需要几步 签到题 #include <algorithm> #include <iostream> #include <cst ...
- Codeforces Round #624 (Div. 3) C. Perform the Combo(前缀和)
You want to perform the combo on your opponent in one popular fighting game. The combo is the string ...
- Codeforces Round #624 (Div. 3) F
题意: 给出n的质点,带着初位置和速度: 如果中途两点可以相遇dis(i,j)=0: 如果不可以相遇,mindis(i,j): 求n个点的两两质点最小dis(i,j)之和 思路: 因为当初位置x和速度 ...
- 详细讲解Codeforces Round #624 (Div. 3) E. Construct the Binary Tree(构造二叉树)
题意:给定节点数n和所有节点的深度总和d,问能否构造出这样的二叉树.能,则输出“YES”,并且输出n-1个节点的父节点(节点1为根节点). 题解:n个节点构成的二叉树中,完全(满)二叉树的深度总和最小 ...
- Codeforces Round #624 (Div. 3) D. Three Integers
You are given three integers a≤b≤ca≤b≤c . In one move, you can add +1+1 or −1−1 to any of these inte ...
随机推荐
- Agri-Net POJ - 1258 prim
#include<iostream> #include<cstdio> #include<cstring> using namespace std; ; #defi ...
- bootstrap-table中时间戳转换为日期格式。
{ field: 'createdTime', title: '创建时间', formatter: function (value, row, index) { return changeDateFo ...
- 浅识mysql主键
primary key:主键,又叫主键约束. primary key在表中是唯一代表一条记录的.primary key可以是1列,或者多列组合而成的. 如何查看一个表的主键是什么,举个例子: desc ...
- mysql之数据初始化update操作
1.单表的:update user set name = (select name from user where id in (select id from user where name='小苏' ...
- java开发就业招聘管理系统 ssh源码
开发环境: Windows操作系统开发工具: MyEclipse+Jdk+Tomcat+MySql数据库 此项目分为 用户 企业 管理员三种角色 运行效果图
- 最大流算法之Ford-Fulkerson算法与Edmonds–Karp算法
引子 曾经很多次看过最大流的模板,基础概念什么的也看了很多遍.也曾经用过强者同学的板子,然而却一直不会网络流.虽然曾经尝试过写,然而即使最简单的一种算法也没有写成功过,然后对着强者大神的代码一点一点的 ...
- VSCode常用插件之open in browser使用
更多VSCode插件使用请访问:VSCode常用插件汇总 open in browser安装完这个插件就可以在编辑器菜单右键html,在默认浏览器打开了,高级使用暂未了解,请自行其它文章学习
- 八连通(vector动态数组法)
题目和一般的八连通一样,但行数和列数未定,相乘对于1e6,直接开a[1e6][1e6]的数组肯定会爆内存.用二维的动态vector就能很好的解决这个问题 #include<bits/stdc++ ...
- ALSA lib-io plugin
https://www.alsa-project.org/alsa-doc/alsa-lib/pcm_external_plugins.html External Plugin: I/O Plugin ...
- 如何在K3查找BOS单据在哪个子系统中
select FFunctionID,* from ICClassType where FName_CHS like '%采购订单%'select FSubSysID,* from t_DataFlo ...