其他分享
首页 > 其他分享> > Trouble Sort(思维)

Trouble Sort(思维)

作者:互联网

传送门

题目要求是将两种数字进行排序

其实该问题的解存在两种情况

情况一:

情况二:

#include <bits/stdc++.h>
using namespace std;

const int N = 505;
int a[N], b[N];
vector<int> vec1, vec0;
void solve(){
	vec1.clear();
	vec0.clear();
	int n;
	cin >> n;
	for(int i = 1; i <= n; i ++) cin >> a[i];
	for(int i = 1; i <= n; i ++) cin >> b[i];
	bool flag0 = false, flag1 = false;
	for(int i = 1; i <= n; i ++){
		if(b[i] == 0)
			vec0.push_back(a[i]);
		else
			vec1.push_back(a[i]);
		if(b[i] == 0)
			flag0 = true;
		else
			flag1 = true;
	}
	int size0 = vec0.size();
	int size1 = vec1.size();
	if(flag0 && !flag1 || !flag0 && flag1){
		if(flag0){
			for(int i = 1; i < size0; i ++){
				if(vec0[i] < vec0[i - 1]){
					cout << "NO" << endl;
					return;
				}
			}
			cout << "YES" << endl;
			return;
		}else{
			for(int i = 1; i < size1; i ++){
				if(vec1[i] < vec1[i - 1]){
					cout << "NO" << endl;
					return;
				}
			}
			cout << "YES" << endl;
			return;
		}
	}
	if(flag0 && flag1)
		cout << "YES" << endl;
}
int main(){
	int t;
	cin >> t;
	while(t --){
		solve();
	}
	
	return 0;
}

标签:Sort,思维,false,int,solve,vec0,vec1,Trouble,种类
来源: https://www.cnblogs.com/pureayu/p/14413449.html