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;
}