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 ...
随机推荐
- 真·从零开始的Ubuntu+Apache+MySQL+PHP+phpstorm+xdebug下的debug环境搭建(纯小白向)
费了好几天劲,终于把Linux下的开发环境如何设置搞的有点明白了,在此写篇博客庆祝一下,让大家以后少踩坑(缓慢更新中) 关键词 win10双系统装Ubuntu 下载U盘烧录工具 下载UbuntuLTS ...
- mysql如何让两个字段数据都不能重复?
目录 场景 任务(需求) 行动(解决方案) 方案1:从代码层面解决(正确方案) 方案2:设置成两个唯一索引(正确方案) 方案3:删掉中间表,把从表的主键作为主表的外键,并将外键设置成唯一索引(正确方案 ...
- IDEA 找不到包或者找不到符号的一些解决办法
有时使用IDE导入项目后,启动时会发生找不到包或者找不到符号的情况,下面有一些处理方法 1.右键项目Maven→Reimport 2.IDEA窗口左上角File→Invalidate and Rest ...
- http断点续传Range与Content-Range
今天用别人封装的libcurl库下载文件,发现下载下来的文件总是缺少头两个字节,用以下配置启用HTTP头信息打印后发现原来是设置了断点续传位置的原因 curl_easy_setopt(m_pCurl, ...
- 《剑指Offer》各面试题总结
目录 前言 面试题4 二维数组的查找 面试题5:替换空格 面试题6:从尾到头打印链表 面试题7:重建二叉树 面试题8:二叉树的下一个节点 面试题9:用两个栈实现队列 面试题10:斐波那契数列 面试题1 ...
- laravel框架使用阿里短信接入
EG: accessKeyid和accessKeySecret还有模板ID.签名名称这几项必要参数自己去阿里云获取一.下载SDK和demo 下载并解压后 在laravel框架的app目录下创建libs ...
- [BOI2003] Gem - 树形dp
结论 不同颜色数不会超过 \(O(\log n)\) 然后就是很简单的树形dp了 顺便复习一下树形dp怎么写 #include <bits/stdc++.h> using namespac ...
- Loj515 「LibreOJ β Round #2」贪心只能过样例 - Bitset,Dp
bitset的基本应用了 类似可行性背包的dp考虑 复杂度O(nmL/64) #include <bits/stdc++.h> using namespace std; bitset &l ...
- PHP实现微信公众号授权获取用户信息
class WxAuthModel extends BaseModel { var $appId = APPID; var $appSecret = APPSECRET; /*微信x小程序,获取微信o ...
- 使用Image Sharp 对二维码图片中间加入logo图片
1.在NuGet包源加入Image Sharp包源 2.在需要用到Image Sharp的项目中安装以下的插件 3.调用图片上绘制图片的方法