当前位置:网站首页>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;
}边栏推荐
- XML小讲
- 优化是一种习惯●出发点是'站在靠近临界'的地方
- 【实用软件】【VSCode】使用技巧大全(持续更新)
- CMU博士论文 | 视频多模态学习:探索模型和任务复杂性
- 壁仞推出全球最大算力芯片,号称以7nm超越英伟达4nm最新GPU
- (10) Sequence and deserialization of image data
- 链表应用----约瑟夫问题
- [SemiDrive source code analysis] [MailBox inter-core communication] 52 - DCF Notify implementation principle analysis and code combat
- Web3中值得关注的基础设施
- [Golang]用反射让你的代码变优美
猜你喜欢
随机推荐
线性结构----链表
Common functions of Auto.js to find pictures and colors
【图像分类】2017-MobileNetV1 CVPR
QSslSocket has not been declared
Kubernetes 笔记 / 入门 / 生产环境 / 用部署工具安装 Kubernetes / 用 kubeadm 启动集群 / 用 kubeadm 创建集群
Transferrin (TF) Modified Paclitaxel (PTX) Liposomes (TF-PTX-LP) | Transferrin (Tf) Modified Curcumin Liposomes
优雅退出在Golang中的实现
如何提高代码的可读性 学习笔记
用汇编带你看Golang里到底有没有值类型、引用类型
Introduction to PostgreSQL
【vulhub】MySql身份认证绕过漏洞复现(CVE-2012-2122)
Go程序员进化史
Ferritin particle-loaded raltitrexed/pemetrexed/sulfadesoxine/adamantane (scientific research reagent)
【网络通信四】ping工具源码cmake工程编译以及运行说明
PostgreSQL — 安装及常用命令
重载和重写
Rider调试ASP.NET Core时报thread not gc-safe的解决方法
PostgreSQL — Installation and Common Commands
实施MES管理系统前,这三个问题要考虑好
win10 xbox录屏功能不能录声音怎么办









