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 ...
随机推荐
- vs2015运行时提示未加载vcruntime140.adm64.pb
后调试查看发现 vs2015运行时提示未加载vcruntime140.adm64.pb 解决方案:去微软官网下载安装 vc_redist.exe ,安装就可以了.有64位版和32位版,根据计算机配置进 ...
- 关于f(x)
有时 z = x + y 有时 0 = x + y 有时单独用f(x) 有时 z = f(x) 很容易分不清. 从集合角度,将f(x)看成映射 即从A集合到B集合的对应关系 这样f(x)可以单独使用, ...
- ansible-yaml语法
1. 基础语法 - yaml结果通过空格来展示 - 数组使用"- "来表示 //注意:-后边要有一个空格 - 键值对使用": "来表示 //注意::后边要有一个 ...
- Pascal运行错误表
(A)DOS错误代码 1:错误的功能代码尝试错误的操作系统调用.2:文件未找到程序试图删除.重命名和打开一个不存在的文件.3:目录未发现目录不存在或是错误,也有可能是访问一个不存在的文件.4:打开太多 ...
- Python中io的open()在PyCharm环境下报错和路劲的问题
PS:我也是初学者,上班空闲时间学习学习Python.今天学到io的时候,遇到了两个用PyCharm环境编写代码的小白错误,如下: 两个问题都是如下代码: 1. 第一个问题:当写好代码之后,点击运行报 ...
- vjudge A Funny Game 思维题 (其实今天讲的全是数学。。。)
原文链接https://vjudge.net/contest/331993#problem/H Alice and Bob decide to play a funny game. At the be ...
- H3C IP路由基础
一.路由简介 在网络中路由器根据所收到的报文的目的地址选择一条合适的路径,并将报文转发到下一个路由器.路径中最后一个路由器负责将报文转发给目的主机. 路由就是报文在转发过程中的路径信息,用来指导报文转 ...
- mysql远程链接(可以在服务器上配置然后在本地连接远程服务器)
ps:如果一下的连接不成功原因:一定要关闭windows防火墙或者linux的防火墙 1.在服务器端授权(黄色标记的地方第一个是用户名,第二个的意思是可以远程连接,第三个是密码) GRANT ALL ...
- Vs2013以管理员身份运行
VS快捷方式目录下的devenv.exe 右键->兼容性疑难解答->疑难解答程序->勾选该程序需要附加权限即可,以后每次打开VS时都会以管理员身份运行了!
- Java基础面试题总结之简述题
一,数组的优缺点 在Java核心技术卷1中写到数组是一种数据结构,用来储存同一类型值的集合. 优点: 1,按照索引查询元素速度快,效率高 2,能存储大量数据 3,按照索引遍历数组方便 缺点: 1,长度 ...