D. Corrupted Array

D. Corrupted Array


D. Corrupted Array
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
You are given a number n and an array b1,b2,…,bn+2, obtained according to the following algorithm:

some array a1,a2,…,an was guessed;
array a was written to array b, i.e. bi=ai (1≤i≤n);
The (n+1)-th element of the array b is the sum of the numbers in the array a, i.e. bn+1=a1+a2+…+an;
The (n+2)-th element of the array b was written some number x (1≤x≤109), i.e. bn+2=x; The
array b was shuffled.
For example, the array b=[2,3,7,12,2] it could be obtained in the following ways:

a=[2,2,3] and x=12;
a=[3,2,7] and x=2.
For the given array b, find any array a that could have been guessed initially.

Input
The first line contains a single integer t (1≤t≤104). Then t test cases follow.

The first line of each test case contains a single integer n (1≤n≤2⋅105).

The second row of each test case contains n+2 integers b1,b2,…,bn+2 (1≤bi≤109).

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output
For each test case, output:

“-1”, if the array b could not be obtained from any array a;
n integers a1,a2,…,an, otherwise.
If there are several arrays of a, you can output any.

Example
input

4
3
2 3 7 12 2
4
9 1 7 1 6 5
5
18 2 2 3 2 9 2
3
2 6 9 2 1

output

2 3 7
-1
2 2 2 3 9
1 2 6

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <algorithm>
using namespace std;



template<typename T>
int erfen(vector<T> arr, int n, long long target) {
    int l = 0, r = n;    //[l,n)
    while (l < r) {         // 当l=r时,[l,r)定义无效
        int mid = l + (r - l) / 2;
        if (arr[mid] == target)
            return mid;
        if (target > arr[mid])
            l = mid + 1;
        else
            r = mid;
    }
    return -1;
}


int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n;i++) {
        int a;
        cin >> a;
        vector <int> b(a + 2);
        long long sum = 0;
        for (int j = 0; j < a + 2; j++) {
            cin >> b[j];
        }
        sort(b.begin(),b.end());
        for (int j = 0; j < a ; j++) {
            sum = sum + b[j];
        }
        int flag = 0;


        if (sum == b[a]) {
            flag ++;
            for (int j = 0; j < a; j++) {
                cout << b[j] << " ";
            }
            cout << endl;
            continue;
        }
        else {
            ;
        }

        //若前n项的和为倒数第二大的元素,则满足题意;否则讨论其他情况

        if (sum == b[a + 1]) {
            for (int j = 0; j < a; j++) {
                cout << b[j] << " ";
            }
            cout << endl;
            continue;
        }
        else {
            sum = sum + b[a];
            int h = erfen(b, a+1, sum- b[a + 1]);
            if (h != -1) {
               for (int j = 0; j < a + 2; j++) {
                   if (j != h && j != a+1)
                       cout << b[j] << " ";
               }
            cout << endl;
            continue;
            }
        }
        //若前n项的和为倒数第一大的元素,则满足题意;
        //否则判断是否存在一个元素与倒数第二大元素换位置后满足
        if (flag==0) {
            cout << -1 << endl;
        }
        //都未找到时,输出-1
    }
}


//错误的想法(就二分而言,未结合实际情况直接使用导致错误)
//数据超过问题(未开 long long,会爆)

暴力不知道能过QwQ(下次试试)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值