You found a useless array aa of 2n2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of aa.
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 xx.
- Then you do the following operation nn times:
- select two elements of array with sum equals xx;
- remove them from aa and replace xx with maximum of that two numbers.
For example, if initially a=[3,5,1,2]a=[3,5,1,2], you can select x=6x=6. Then you can select the second and the third elements of aa with sum 5+1=65+1=6 and throw them out. After this operation, xx equals 55 and there are two elements in array: 33 and 22. You can throw them out on the next operation.
Note, that you choose xx 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 aa.
Input
The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases.
The first line of each test case contains the single integer nn (1≤n≤10001≤n≤1000).
The second line of each test case contains 2n2n integers a1,a2,…,a2na1,a2,…,a2n (1≤ai≤1061≤ai≤106) — the initial array aa.
It is guaranteed that the total sum of nn over all test cases doesn't exceed 10001000.
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 xx you've chosen. Print description of nn operations next. For each operation, print the pair of integers you remove.
Example
input
Copy
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
output
Copy
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 aa.
思路:
1,这题规律我找到了,但是stl没有选择对,这种情况可以让队友去写
2,multiset有find(数)返回指针,erase(指针)的操作,
代码:
int a[2200],n;
vec check(int x){
vec ve;
multiset<int> se;
rep(i,1,n)se.emplace(a[i]);
rep(i,1,n/2){
auto it = se.end();--it;
int y=x-*it;
se.erase(it);
auto ii=se.find(y);//set会find数,并返回指针
if(ii==se.end())return {};//找不到,返回end()
se.erase(ii);//删除指针
ve.emplace_back(y);
ve.emplace_back(x-y);
x=max(y,x-y);
}
return ve;
}
void solve(){//找到规律,用合适的结构去暴力枚举
cin>>n;
n<<=1;
rep(i,1,n)cin>>a[i];
sort(a+1,a+1+n);
rep(i,1,n-1){//遍历所有可能的初始值
int x=a[i]+a[n];
vec ve=check(x);
if(ve.size()){
cout<<"YES"<<'\n'<<x<<'\n';
for(int i=0;i<ve.size()-1;i+=2){
cout<<ve[i]<<' '<<ve[i+1]<<'\n';
}
return ;
}
}cout<<"NO"<<'\n';
}
标签:aa,elements,ve,stl,array,Array,Destruction,throw,out
From: https://blog.csdn.net/m0_63054077/article/details/125897626