其他分享
首页 > 其他分享> > Codeforces Round #696 (Div. 2) C. Array Destruction (贪心,multiset)

Codeforces Round #696 (Div. 2) C. Array Destruction (贪心,multiset)

作者:互联网


#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
#define rep(a,b,c) for(int a=b;a<=c;++a)
#define per(a,b,c) for(int a=b;a>=c;--a)
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b) {return a/gcd(a,b)*b;}

int _;

int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin>>_;
	while(_--){
		int n;
		cin>>n;
		vector<int> a(2*n);
		for(auto &w:a) cin>>w;

		sort(a.rbegin(),a.rend());

		bool ok=false;
		
		rep(i,1,2*n-1){
			multiset<int>s;
			rep(j,1,2*n-1) {if(i!=j) s.insert(a[j]);}
			vector<PII> ans;
			int mx=a[0];
			int sum=a[0]+a[i];
			ans.pb({a[0],a[i]});
			bool flag=true;
			while(!s.empty() && flag){
				auto it=s.end();
				it--;
				s.erase(it);
				int cur=*it; //greedy,choose the max number(only way)
				
				auto it_=s.lower_bound(mx-cur);
				if(it_==s.end()) {flag=false;break;}	
				int cur_=*it_;

				if(cur+cur_==mx){
					ans.pb({cur,cur_});
					mx=cur;
					s.erase(it_);
				}
				else{
					flag=false;
					break;
				}
			}
			if(flag){
				cout<<"YES\n";
				cout<<sum<<'\n';
				for(auto [x,y]:ans) cout<<x<<' '<<y<<'\n';
				ok=true;
				break;
			}
		}

		if(!ok) cout<<"NO\n";
				
	}


    return 0;
}

标签:696,cur,int,ll,Codeforces,mx,flag,Round,define
来源: https://www.cnblogs.com/lr599909928/p/14305721.html