結果

問題 No.2307 [Cherry 5 th Tune *] Cool 46
ユーザー shobonvip
提出日時 2023-05-19 21:38:57
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 1,976 bytes
コンパイル時間 4,338 ms
コンパイル使用メモリ 257,188 KB
最終ジャッジ日時 2025-02-13 01:33:36
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef modint998244353 mint;
typedef long long ll;

// importbisect
template <typename T>
int bisect_left(vector<T> &X, T v){
	return lower_bound(X.begin(), X.end(), v) - X.begin();
}

template <typename T>
int bisect_right(vector<T> &X, T v){
	return upper_bound(X.begin(), X.end(), v) - X.begin();
}
// -----


void solve(){
	int n, m; cin >> n >> m;
	vector<int> a(n), b(m);
	for (int i=0; i<n; i++){
		cin >> a[i];
	}
	for (int i=0; i<m; i++){
		cin >> b[i];
	}

	sort(a.begin(), a.end());
	sort(b.begin(), b.end());

	if (n == 0){
		cout << "Yes\n";
		for (int i=0; i<m; i++){
			cout << "Blue " << b[i] << "\n";
		}
		return;
	}

	if (m == 0){
		cout << "Yes\n";
		for (int i=0; i<n; i++){
			cout << "Red " << a[i] << "\n";
		}
		return;
	}
	
	vector<pair<int,int>> goodpair;
	vector<bool> usered(n), useblue(m);
	bool can = false;

	for (int i=0; i<n; i++){
		int tmp = bisect_left(b, a[i]);
		if (tmp < m && b[tmp] == a[i]){
			usered[i] = true;
			useblue[tmp] = true;
			goodpair.push_back(pair(i, tmp));
			can = true;
		}
	}

	if (!can){
		cout << "No\n";
		return;
	}

	cout << "Yes\n";

	for (int i=0; i<n; i++){
		if (!usered[i]){
			cout << "Red " << a[i] << "\n";
		}
	}

	int x = goodpair.back().first;
	int y = goodpair.back().second;

	goodpair.pop_back();

	cout << "Red " << a[x] << "\n";
	cout << "Blue " << b[y] << "\n";
	
	for (int i=0; i<m; i++){
		if (!useblue[i]){
			cout << "Blue " << b[i] << "\n";
		}
	}

	bool mode = true;
	while(!goodpair.empty()){
		x = goodpair.back().first;
		y = goodpair.back().second;

		goodpair.pop_back();
		if (mode){
			cout << "Blue " << b[y] << "\n";
			cout << "Red " << a[x] << "\n";
			mode = false;
		}else{
			cout << "Red " << a[x] << "\n";
			cout << "Blue " << b[y] << "\n";
			mode = true;
		}
	}
	
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	int t; cin >> t;
	while(t--) solve();
}
0