Codeforces Round #624 (Div. 3) B - WeirdSort
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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.
Output
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".
Example
input
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
output
YES
NO
YES
YES
NO
YES
此题有很多做法,我用的是并查集,其实也可以用冒泡排序的思想来做,最后再检查是否有序即可
并查集的思路也很简单就是题目给的可交换的下标都可以合在一起,然后去检查排好序之后的下标和原先的下标是不是在一个集合内就ok了
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rg register ll
#define inf 2147483647
#define lb(x) (x&(-x))
ll sz[200005],n;
template <typename T> inline void read(T& x)
{
x=0;char ch=getchar();ll f=1;
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}x*=f;
}
inline ll query(ll x){ll res=0;while(x){res+=sz[x];x-=lb(x);}return res;}
inline void add(ll x,ll val){while(x<=n){sz[x]+=val;x+=lb(x);}}//第x个加上val
struct node
{
ll val,pos;
}p[105];
inline bool cmp(const node&a,const node&b)
{
if(a.val==b.val)return a.pos<b.pos;
return a.val<b.val;
}
ll t,f[105],q[105];
inline void init(ll m)
{
memset(f,0,sizeof(f));
for(rg i=1;i<=100;i++)f[i]=i;
}
inline ll fa(ll x)
{
return f[x]==x?x:f[x]=fa(f[x]);
}
inline void merge(ll x,ll y)
{
ll fx=fa(x),fy=fa(y);
if(fx!=fy)
{
f[fy]=fx;
}
}
int main()
{
cin>>t;
for(rg i=1;i<=t;i++)
{
ll n,m,sum=0;
cin>>n>>m;
init(m);
for(rg i=1;i<=n;i++)cin>>p[i].val,p[i].pos=i;
for(rg j=1;j<=m;j++)cin>>q[j],merge(q[j],q[j]+1);
sort(p+1,p+1+n,cmp);
for(rg j=1;j<=n;j++)
{
if(fa(p[j].pos)==fa(j))
{
sum++;
}
}
sum==n?cout<<"YES"<<endl:cout<<"NO"<<endl;
}
while(1)getchar();
return 0;
}
/*
3 2 1
1 2
*/
- 《像计算机科学家一样思考Java》—— 读后总结
- 记录安装oracle的那些事(三)之oracle Database R2安装
- Elasticsearch Javascript API增删改查
- Oracle二三事之 Oracle SPARC SuperCluster的九大技术优势
- 两个 viewports 的故事-第二部分
- 通过 JS 实现简单的拖拽功能并且可以在特定元素上禁止拖拽
- AngularJS 技术总结
- 《linux c编程指南》学习手记5
- AngularJS API之bootstrap启动
- 通过 JS 判断页面是否有滚动条的简单方法
- Log4j官方文档翻译(六、日志的级别)
- AngularJS API之isXXX()
- 《linux c编程指南》学习手记4
- Kibana中doc与search策略的区别
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 轻松构建Tomcat源码
- Flutter中Contrainer 组件的宽高限制分析
- 10张图带你深入理解Docker容器和镜像
- 手摸手教你撸一个微服务框架-关于服务端的处理
- 聊聊claudb的string command
- windows下安装nodejs
- 【Java面试总结】Java集合
- 《JavaScript 模式》读书笔记(8)— DOM和浏览器模式1
- 《JavaScript 模式》读书笔记(8)— DOM和浏览器模式2
- 5000字 | 24张图带你彻底理解21种并发锁
- JavaScript-变量
- Android应用安装卸载监控
- 细数这些年被困扰过的 TS 问题
- 将WordPress插件Elementor标签插入到WordPress模板文件以使用Elementor编辑
- WordPress自定义新建多区域widget小工具调用