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 ...
随机推荐
- 汇编语言中LABEL伪指令的功能?
LABEL 一般用作定义变量和标号的属性,它是与紧接着的下一条变量和标号定义语句相关的,其类型可以为BYTE.WORD.DWORD.QWORD.NEAR.FAR等等.用法为:buffer(变量) LA ...
- F. Moving Points
https://codeforces.com/contest/1311/problem/F 这是一道线段树类型的题: 可以用权值线段树或者树状数组来解: 所以,我们可以分为两部分,第一部分是计算出到当 ...
- a++ 和 ++a
//a++ 先赋值,后加 var a = 1 console.log(a++);//1 console.log(a) //2 //++a 先赋值,后加 var a = 1 console.log(++ ...
- Git和TortoiseGit
1.简介 Git是一个开源的分布式版本控制系统,用于敏捷高效的处理任何或大或小的项目.它采用了分布式版本库的方式,不必服务器端软件支持. 2.Git和Svn的区别 1.Git 是分布式的,SVN 不是 ...
- 最短路-B - 六度分离
B - 六度分离 1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为“小世界现象(small world phenomenon)”的著名假说,大意是说,任何2个素不相识的人中间最多只隔着6 ...
- 【E20200102-1】centos 7 下vsftp的安装和配置
一.准备工作 1.1.服务器准备 操作系统:centos 7.x 关闭防火墙(firewall/iptables)和SELinux 参见笔记<[E20200101-1]Centos 7.x 关闭 ...
- 题解【洛谷P3406】海底高铁
题面 比较基础的前缀和+差分. 注意开\(\text{long long}\) 直接上代码吧. #include <bits/stdc++.h> #define itn int #defi ...
- Vim入门——Windows下安装
下载页面:https://www.vim.org/download.php Windows选用的是MS-Windows: 下图为展示: 因为最近被墙,镜像貌似没中国内陆地区,因此,选择使用GitHub ...
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- android TextView 支持长按自由复制
因为EditText支持系统的长按自由复制,所以只需要把EditText通过配置达到TextView效果就行了 <EditText android:id="@+id/subject_i ...