【Codeforces 1474 C】Array Destruction,枚举,multiset模拟

problem

C. Array Destruction
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You found a useless array a of 2n positive integers. You have realized that you actually don’t need this array, so you decided to throw out all elements of a.

It could have been an easy task, but it turned out that you should follow some rules:

In the beginning, you select any positive integer x.
Then you do the following operation n times:
select two elements of array with sum equals x;
remove them from a and replace x with maximum of that two numbers.
For example, if initially a=[3,5,1,2], you can select x=6. Then you can select the second and the third elements of a with sum 5+1=6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation.

Note, that you choose x before the start and can’t change it as you want between the operations.

Determine how should you behave to throw out all elements of a.

Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases.

The first line of each test case contains the single integer n (1≤n≤1000).

The second line of each test case contains 2n integers a1,a2,…,a2n (1≤ai≤106) — the initial array a.

It is guaranteed that the total sum of n over all test cases doesn’t exceed 1000.

Output
For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise.

If it is possible to throw out all elements, print the initial value of x you’ve chosen. Print description of n operations next. For each operation, print the pair of integers you remove.

Example
inputCopy
4
2
3 5 1 2
3
1 1 8 8 64 64
2
1 1 2 4
5
1 2 3 4 5 6 7 14 3 11
outputCopy
YES
6
1 5
2 3
NO
NO
YES
21
14 7
3 11
5 6
2 4
3 1
Note
The first test case was described in the statement.

In the second and third test cases, we can show that it is impossible to throw out all elements of array a.

solution

/*
题意:
+ 给出一个长为2n的数组,每次从数组中删去和为x的两个数,并令下一轮的x为两者的较大值,初始时x任意指定。
+ 求构造出可行的初始x和方案能删去数组中的所有元素。
思路:
+ 对于排好序的数组a[1-2n],假设初始x=第2,3大数的和,那么第二轮的x等于第2大的数,最大的那个永远也无法删掉,所以x肯定是最大的数加一个数构成的。
+ O(n)枚举可能的初始x,然后模拟判断是否存在可行方案。
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2020;
const int mod = 1e9+7;
int a[maxn], n;
vector<int> check(int x){
	multiset<int>s; //维护可重集合的最大值,查找,删除。
	for(int i = 1; i <= n; i++)s.insert(a[i]);
	vector<int>res;
	for(int i = 1; i <= n/2; i++){//删除n次
		multiset<int>::iterator it1 = s.end();  it1--;//取最大
		int y = x-*it1;//y+x==_max
		s.erase(it1);
		multiset<int>::iterator it2 = s.find(y);//如果不存在则无解
		if(it2 == s.end())return {};
		s.erase(it2);
		
		res.push_back(y);
		res.push_back(x-y);
		x = max(y,x-y);
	}
	return res;
}
int main(){
	int T;  cin>>T;
	while(T--){
		cin>>n;  n*=2;
		for(int i = 1; i <= n; i++)cin>>a[i];
		sort(a+1,a+n+1);
		int ok = 0;
		for(int i = 1; i < n; i++){
			int x = a[i]+a[n];
			vector<int>res = check(x);
			if(res.size()){
				cout<<"YES\n"<<x<<"\n";
				for(int i = 0; i < res.size(); i+=2){
					cout<<res[i]<<" "<<res[i+1]<<"\n";
				}
				ok = 1; break;
			}
		}
		if(ok==0)cout<<"NO\n";
	}
	return 0;
}


已标记关键词 清除标记
相关推荐
<p> <strong><span style="font-size:20px;color:#FF0000;">本课程主要针对计算机相关专业的正在做毕设的学生与需要项目实战练习的Java学习者</span></strong> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">1. 包含:<span style="color:#FFFF00;background-color:#FF0000;">项目源码、</span><span style="color:#FFFF00;background-color:#FF0000;">项目文档、数据库脚本、软件工具</span>等所有资料</span></strong></span> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">2. 手把手的带你从零开始部署运行本套系统</span></strong></span> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">3. 该项目附带的源码资料可作为毕设使用</span></strong></span> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">4. 提供技术答疑和远程协助指导</span></strong></span><strong><span style="font-size:18px;"></span></strong> </p> <p> <br /> </p> <p> <span style="font-size:18px;"><strong>项目运行截图:</strong></span> </p> <p> <strong><span style="font-size:18px;">1)系统登陆界面</span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241015433522.png" alt="" /><br /> </span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">2)学生模块</span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241015575966.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">3)教师模块</span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241016127898.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">4)系统管理员</span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241016281177.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241016369884.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><br /> </span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">更多Java毕设项目请关注我的毕设系列课程 <a href="https://edu.csdn.net/lecturer/2104">https://edu.csdn.net/lecturer/2104</a></span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><br /> </span></strong> </p>
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页