专栏文章

题解:CF1474C Array Destruction

CF1474C题解参与者 1已保存评论 0

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
0 条
当前快照
1 份
快照标识符
@mioelf18
此快照首次捕获于
2025/12/02 17:56
3 个月前
此快照最后确认于
2025/12/02 17:56
3 个月前
查看原文

Solution

对于这道题的 xx,我们只能选择数组中最大的数字与其他一个数字的和,否则更大的就无法删除。
每次选择较大的数 aia_i,另一个数就是 xaix - a_i,若 xaix - a_i 不存在,即此方案无解。否则将 xx 设为 aia_i,记录答案,然后一直循环此过程到结束或无解。
若所有方案都无解,输出 NO
CPP
#include <bits/stdc++.h>

using namespace std;

const int N = 2005;

int n, a[N], cnt;

struct Ans {
	int x, y;
} ans[N];

bool check(int idx, unordered_map<int, int> mp) {
	cnt = 1;
	ans[cnt] = {a[idx], a[n]};
	int x = a[n];
	mp[a[idx]]--, mp[a[n]]--;
	for (int i = n - 1; cnt < n / 2; i--) {
		if (mp[a[i]] == 0)
			continue;
		if ((a[i] == x / 2 && mp[a[i]] < 2) || (mp[x - a[i]] == 0)) {
			return 0;
		}
		mp[a[i]]--, mp[x - a[i]]--;
		ans[++cnt] = {a[i], x - a[i]};
		x = a[i];
	}
	return cnt == n / 2;
}

signed main() {
	cin.tie(0), cout.tie(0)->sync_with_stdio(false);
	int t;
	for (cin >> t; t--;) {
		cin >> n;
		n <<= 1;
		unordered_map<int, int> mp;
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
			mp[a[i]]++;
		}
		sort (a + 1, a + 1 + n);
		bool f = 0;
		int idx;
		for (int i = 1; i <= n - 1; i++) {
			if (check(i, mp)) {
				f = 1;
				idx = i;
				break;
			}
		}
		if (f) {
			cout << "YES\n";
			cout << a[n] + a[idx] << '\n';
			for (int i = 1; i <= cnt; i++) {
				cout << ans[i].x << ' ' << ans[i].y << '\n';
			}
		} else {
			cout << "NO\n";
		}
	}
	return 0;
} 

评论

0 条评论,欢迎与作者交流。

正在加载评论...