codeforces 1474 C

题目链接:https://codeforces.com/contest/1474/problem/C

枚举第一次是用哪一个和最大值组成 (x), 此后每一次都要选当前序列中最大值做为 (x)
使用 (multiset) 即可

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

const int maxn = 1010;

int T, n;
int a[maxn << 1];

bool solve(int t){
	multiset<int> s;
	vector<pair<int, int> > ans;
	
	for(int i = 1 ; i <= 2 * n ; ++i) s.insert(a[i]);
	
	int x = a[t] + a[2 * n];
	
	while(!s.empty()){
		auto it1 = s.end();
		--it1;
		s.erase(it1);
		int y = x - *it1;
		
		auto it2 = s.find(y);
		if(it2 == s.end()){
			return false;
		}
		
		s.erase(it2);
		ans.push_back(make_pair(y, x - y));
		x = max(y, x - y);
	}
	
	printf("YES
");
	printf("%d
", a[t] + a[2 * n]);
	for(int i = 0 ; i < ans.size() ; ++i){
		printf("%d %d
", ans[i].first, ans[i].second);
	}
}

ll read(){ ll s = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); } return s * f; }

int main(){
	T = read();
	while(T--){
		n = read();
		for(int i = 1 ; i <= 2 * n ; ++i) a[i] = read();
		
		sort(a + 1, a + 1 + 2 * n); 
		
		int f = 0;
		for(int i = 1 ; i < 2 * n ; ++i) {
			if(solve(i)) {
				f = 1;
				break;
			}
		}
		if(!f){
			printf("NO
");
		}
	} 
	return 0;
}
原文地址:https://www.cnblogs.com/tuchen/p/14327176.html