C. Matching Arrays

链接:https://codeforces.com/problemset/problem/1896/C
洛谷:https://www.luogu.com.cn/problem/CF1896C
这题疑似有点水了?为什么还有绿题hhhh
思路:结构体+排序
首先对a,b各自排序:取b的下x和a的上x比较,如果可以(指ai>bi),那么进入二阶段;如果不行,那么直接输出no。
二阶段:取b的上n-x和a的下n-x比较,如果可以(指ai<=bi)那么输出yes,并且根据id重排序,输出每个b;否则输出no;
代码:

#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
#include<set>
typedef long long ll;
using namespace std;
const int N = 2e5 + 10;
struct ea
{
	ll a, b, id;
	ea() { a = b = id = 0; }
};
ll blst[N];
bool cmp_by_id(ea a, ea b)
{
	return a.id < b.id;
}
bool cmp_by_val(ea a, ea b)
{
	return a.a < b.a;
}
ea ealst[N];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int t; cin >> t;
	while (t--)
	{
		int n; cin >> n;
		int x; cin >> x;
		memset(blst, 0, sizeof(blst));
		for (int i = 1; i <= n; i++)
		{
			ealst[i].id = i;
			cin >> ealst[i].a;
		}
		for (int i = 1; i <= n; i++)
		{
			cin >> blst[i];
		}
		sort(blst + 1, blst + 1 + n);
		sort(ealst + 1, ealst + 1 + n, cmp_by_val);
		bool isend = true;
		for (int i = 1; i <= x; i++)
		{
			if (ealst[n - x + i].a > blst[i])
			{
				ealst[n - x + i].b = blst[i];

			}
			else
			{
				isend = false;
				break;
			}
		}
		if (!isend);
		else
		{
			for (int i = 1; i <= n-x; i++)
			{
				if (ealst[i].a > blst[x + i])
				{
					isend = false; break;
				}
				else ealst[i].b = blst[x + i];
			}
		}
		if (!isend)
		{
			cout << "NO" << endl;
		}
		else
		{
			sort(ealst + 1, ealst + 1 + n, cmp_by_id);
			cout << "YES" << endl;
			for (int i = 1; i <= n; i++)cout << ealst[i].b << ' ';
			cout << endl;
		}
	}
	return 0;
}

posted on 2024-05-01 10:50  WHUStar  阅读(5)  评论(0编辑  收藏  举报