当前位置:网站首页>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;
}边栏推荐
- 测试开发【Mock 平台】08 开发:项目管理(四)编辑功能和Component抽离
- ansible各个模块的详解和使用
- Transferrin-modified osthole long-circulating liposomes/PEG-PLGA nanoparticles loaded with notoginsenoside R1 ([email prot
- 线性结构----链表
- 2021DASCTF实战精英夏令营暨DASCTF July X CBCTF 4th
- Auto.js找图找色常用功能
- 优化是一种习惯●出发点是'站在靠近临界'的地方
- The servlet mapping path matching resolution
- 什么是抽象类?什么时候用?什么是接口?抽象类与接口的区别?
- 数据标注太昂贵?这个方法可以用有限的数据训练模型实现基于文本的ReID!
猜你喜欢

paddle 35 paddledetection保存训练过程中的log信息

内置模板市场,DataEase开源数据可视化分析平台v1.13.0发布

【图像分类】2019-MoblieNetV3 ICCV

Tf ferritin particles contain cisplatin / oxaliplatin / doxorubicin / methotrexate MTX / paclitaxel PTX and other drugs

【语义分割】2017-PSPNet CVPR

大小端的理解以及宏定义实现的理解

详叙c中的分支与循环
![[SWPUCTF 2021 新生赛] web](/img/e9/07e7db7ddf8328589a078e98fd46ad.png)
[SWPUCTF 2021 新生赛] web

图扑智慧电力可视化大屏,赋能虚拟电厂精准减碳

"Distributed Microservice E-commerce" Topic (1) - Project Introduction
随机推荐
[mysql] 深入分析MySQL版本控制MVCC规则
"POJ 3666" Making the Grade problem solution (two methods)
壁仞推出全球最大算力芯片,号称以7nm超越英伟达4nm最新GPU
Getting started with kuberentes Auditing
2021 CybricsCTF
Web3中值得关注的基础设施
(10) Sequence and deserialization of image data
面向未来的 IT 基础设施管理架构——融合云(Unified IaaS)
svg+元素js实现在图片上描点成框,并获取相对图片的坐标位置
洛谷 P1629 邮递员送信 (三种最短路)
关于 NFT 版权保护的争议
Introduction to PostgreSQL
Tf ferritin particles contain cisplatin / oxaliplatin / doxorubicin / methotrexate MTX / paclitaxel PTX and other drugs
设备管理中数据聚类处理
Common functions of Auto.js to find pictures and colors
Transferrin-modified vincristine-tetrandrine liposomes | transferrin-modified co-loaded paclitaxel and genistein liposomes (reagents)
图数据库(Neo4j)入门
Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
ansible各个模块的详解和使用
sklearn 笔记 TSNE