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


  • 题意:有(n)个数,首先任选一个正整数(x),然后在数组中找到两个和为(x)的数,然后去掉这两个数,(x)更新为两个数中较大的那个.问你最后时候能把所有数都去掉,如果能,输出最初的(x)和每次去除的两个数.
  • 题解:首先现将数组排序,我们每次肯定都是要用数组中当前最大的数和其他数进行组合,这很容易证明,假如我们选两个比它小的数,那么更新后的(x)一定永远小于最大值,那么它就永远用不掉,那么第一次的(x)一定是数组的最大值和某个数的组合,我们可以枚举来处理,然后剩下的过程用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
";
				cout<<sum<<'
';
				for(auto [x,y]:ans) cout<<x<<' '<<y<<'
';
				ok=true;
				break;
			}
		}

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


    return 0;
}

原文地址:https://www.cnblogs.com/lr599909928/p/14305721.html