当前位置:网站首页>B. Trouble Sort
B. Trouble Sort
2022-08-10 20:44:00 【秦小咩】
B. Trouble Sort
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Ashish has nn elements arranged in a line.
These elements are represented by two integers aiai — the value of the element and bibi — the type of the element (there are only two possible types: 00 and 11). He wants to sort the elements in non-decreasing values of aiai.
He can perform the following operation any number of times:
- Select any two elements ii and jj such that bi≠bjbi≠bj and swap them. That is, he can only swap two elements of different types in one move.
Tell him if he can sort the elements in non-decreasing values of aiai after performing any number of operations.
Input
The first line contains one integer tt (1≤t≤100)(1≤t≤100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer nn (1≤n≤500)(1≤n≤500) — the size of the arrays.
The second line contains nn integers aiai (1≤ai≤105)(1≤ai≤105) — the value of the ii-th element.
The third line containts nn integers bibi (bi∈{0,1})(bi∈{0,1}) — the type of the ii-th element.
Output
For each test case, print "Yes" or "No" (without quotes) depending on whether it is possible to sort elements in non-decreasing order of their value.
You may print each letter in any case (upper or lower).
Example
input
Copy
5 4 10 20 20 30 0 1 0 1 3 3 1 2 0 1 1 4 2 2 4 8 1 1 1 1 3 5 15 4 0 0 0 4 20 10 100 50 1 0 0 1
output
Copy
Yes Yes Yes No Yes
Note
For the first case: The elements are already in sorted order.
For the second case: Ashish may first swap elements at positions 11 and 22, then swap elements at positions 22 and 33.
For the third case: The elements are already in sorted order.
For the fourth case: No swap operations may be performed as there is no pair of elements ii and jj such that bi≠bjbi≠bj. The elements cannot be sorted.
For the fifth case: Ashish may swap elements at positions 33 and 44, then elements at positions 11 and 22.
=========================================================================
首先如果我们能够两两之间任意交换,那么肯定,我们一定能够排序
可是题目给我们限定为两个b不相同的才能交换,那么就代表我们不能两两交换了吗?
错!我们依然能够两两交换
a b c
1 0 1
a与c可以在不改变b位置的情况下进行交换
方法是 b移动到位置3,c移动到位置2,a移动到位置3,b移动到位置1,c移动到位置1,b回归位置2,完成交换。
所以只需要有一个不同的b,我们就可以完全进行交换
反之,要是一个不同的b也没有,那么我们只能祈祷原数组是不减的了
#include<iostream>
#include<cstdio>
#include<cstring>
# include<iomanip>
#include<algorithm>
#define mo 998244353;
using namespace std;
typedef long long int ll;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int x,pre;
cin>>x;
pre=x;
int flag1=0,flag2=0,flag3=0;
for(int i=2;i<=n;i++)
{
int x;
cin>>x;
if(x<pre)
{
flag1=1;
}
pre=x;
}
for(int i=1;i<=n;i++)
{
cin>>x;
if(x)
flag2=1;
else
flag3=1;
}
if(flag1==0)
{
cout<<"YES"<<endl;
}
else
{
if(flag2+flag3==1)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
}
}
}
return 0;
}
边栏推荐
- svg+元素js实现在图片上描点成框,并获取相对图片的坐标位置
- TortoiseSVN小乌龟的使用
- Ransom Letter Questions and Answers
- XML小讲
- npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
- 饿了么-机构树单选
- Transferrin-modified vincristine-tetrandrine liposomes | transferrin-modified co-loaded paclitaxel and genistein liposomes (reagents)
- 双 TL431 级联振荡器
- Demis Hassabis:AI 的强大,超乎我们的想象
- (十二)STM32——NVIC中断优先级管理
猜你喜欢
Ransom Letter Questions and Answers
npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
Web3中值得关注的基础设施
姜还是老的辣,看看老战哥的老底儿和严谨劲儿
npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
组合导航精度分析
ES6中的for...in/of的使用
- [email protected] nanomimetic e"/>
Water-soluble alloy quantum dot nanozymes|CuMoS nanozymes|porous silicon-based Pt(Au) nanozymes|[email protected] nanomimetic e
TortoiseSVN小乌龟的使用
找的笔试题的复盘(一)
随机推荐
YOLOv3 SPP source analysis
【CMU博士论文】视频多模态学习:探索模型和任务复杂性,152页pdf
kuberentes Auditing 入门
Web3中值得关注的基础设施
Rider调试ASP.NET Core时报thread not gc-safe的解决方法
.NET现代应用的产品设计 - DDD实践
【实用软件】【VSCode】使用技巧大全(持续更新)
Echart饼状图标注遮盖解决方案汇总
Water-soluble alloy quantum dot nanozymes|CuMoS nanozymes|porous silicon-based Pt(Au) nanozymes|[email protected] nanomimetic e
优雅退出在Golang中的实现
The 2021 ICPC Asia Shanghai Regional Programming Contest D、E
姜还是老的辣,看看老战哥的老底儿和严谨劲儿
第五届“强网杯”全国网络安全挑战赛(线上赛)
第14届全国大学生信息安全竞赛-创新实践能力赛
Kubernetes 笔记 / 入门 / 生产环境 / 用部署工具安装 Kubernetes / 用 kubeadm 启动集群 / 用 kubeadm 创建集群
C语言写数据库
C 语言 时间函数使用技巧(汇总)
MySQL查询数据库中的表和字段
mysql服务器参数设置
Kerberos认证