Array Destruction

题目链接:链接: codeforces—1474C.
题目描述:
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:
input:
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:
YES
6
1 5
2 3
NO
NO
YES
21
14 7
3 11
5 6
2 4
3 1

题意:有一个大小为2*n的数组,每次从数组中挑出2个数,其中最大的数为下一次挑出的两个数的和,问是否数组能否挑完。
思路:n的范围比较小,可以直接暴力。首先将数组排序,然后命第一对数中的最大值为数组的最大值,然后从剩余的数组中挑算一个与它匹配。当前两个数的最大值,为下一对数的和,下一对数中的最大值为数组剩余数中的最大值,同时可以求出下一对数中的较小值。当挑选出n对数时,结束循环,输出答案。

#include<bits/stdc++.h>
#include<iostream>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
typedef unsigned long long ull;
typedef  long long ll;
#define IOS std::ios::sync_with_stdio(false)
#define ls p<<1
#define rs p<<1|1
#define mod 1000000000 + 7
#define PI acos(-1.0)
#define INF   1e9
#define N 2000000 + 5
/*********************Code*********************/
int t,n,a[N],ans[N],cnt[N];
bool flag;
int main(void){
    IOS;
    cin>>t;
    while(t--){
        memset(cnt,0,sizeof(cnt));
        flag = false;
        cin>>n;
        for(int i = 1;i <=2*n;i++)
            cin>>a[i];
        sort(a+1,a+2*n+1);
        for(int i = 1;i < 2*n&&!flag;i++){
            for(int j = 1;j <=2*n;j++)
                cnt[a[j]]++;
            int m = 2 *n,pos = 0;
            int x = a[2*n] + a[i];
            for(int j = 1;j <=n;j++){
                while(m>1&&cnt[a[m]]==0)
                    m--;
                pos++,ans[pos] = a[m];
                pos++,ans[pos] =x-a[m];
                cnt[a[m]]--,cnt[x-a[m]]--;
                if(cnt[a[m]]<0||cnt[x-a[m]]<0)
                    break;
                x = max(a[m],x-a[m]);
                if(j==n)
                    flag = true;
            }
            memset(cnt,0,sizeof(cnt));
            if(flag){
                cout<<"YES"<<endl;
                cout<<ans[1]+ans[2]<<endl;
                for(int i = 1;i <=2*n;i+=2)
                    cout<<ans[i]<<" "<<ans[i+1]<<endl;
            }
        }
        if(!flag)
            cout<<"NO"<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值