当前位置:网站首页>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;
}
边栏推荐
- Transferrin-modified osthole long-circulating liposomes/PEG-PLGA nanoparticles loaded with notoginsenoside R1 ([email prot
- 将视图模型转换为使用 Hilt 依赖注入
- Rider调试ASP.NET Core时报thread not gc-safe的解决方法
- 什么是抽象类?什么时候用?什么是接口?抽象类与接口的区别?
- apr_thread使用内存之谜
- leetcode:45. 跳跃游戏II
- The 2021 ICPC Asia Shanghai Regional Programming Contest D、E
- 饿了么-机构树单选
- paddle 35 paddledetection保存训练过程中的log信息
- 图数据库(Neo4j)入门
猜你喜欢
Pt/CeO2 monatomic nanoparticles enzyme | H - rGO - Pt @ Pd NPs enzyme | carbon nanotube load platinum nanoparticles peptide modified nano enzyme | leukemia antagonism FeOPtPEG composite nano enzyme
Kerberos认证
爱丁堡大学最新《因果机器学习: 医疗健康与精准医疗应用》2022综述
单选点击可取消功能
win7开机有画面进系统黑屏怎么办
组合导航精度分析
【图像分类】2017-MobileNetV1 CVPR
链表应用----约瑟夫问题
The 2021 ICPC Asia Shanghai Regional Programming Contest D、E
找的笔试题的复盘(一)
随机推荐
如何提交一个PR?【OpenHarmony成长计划】【OpenHarmony开源社区】
npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
Heme - gold nanoparticles (Heme - AuNP) composite nanometer enzyme | gold nanoparticles nuclear porous hollow carbon nanometer spherical shell (Au @ HCNs) nano enzyme
(十二)STM32——NVIC中断优先级管理
【语义分割】2017-PSPNet CVPR
.NET现代应用的产品设计 - DDD实践
Date picker component (restrict year to set only displayed months)
Iridium Ruthenium Alloy/Iridium Oxide Biomimetic Nanozyme | Palladium Nanozyme | GMP-Pd Nanozyme | Gold-Palladium Composite Nanozyme | Ternary Metal Pd-M-Ir Nanozyme |shell nanozyme
echart 特例-多分组X轴
[SWPUCTF 2021 新生赛] web
Ferritin particle-loaded raltitrexed/pemetrexed/sulfadesoxine/adamantane (scientific research reagent)
OPPO Enco X2 迎来秋季产品升级 旗舰体验全面拉满
【图像分类】2017-MobileNetV1 CVPR
UE4 - 河流流体插件Fluid Flux
2019河北省大学生程序设计竞赛部分题题解
[SemiDrive source code analysis] [MailBox inter-core communication] 51 - DCF_IPCC_Property implementation principle analysis and code combat
ansible各个模块的详解和使用
社区分享|货拉拉通过JumpServer纳管大规模云上资产
mysql服务器参数设置
Rider调试ASP.NET Core时报thread not gc-safe的解决方法