链接:
https://codeforces.com/problemset/problem/1512/D
题意:
给一个数组b,有n+2个数字。如果能找到n个数字的和,等于其中另一个数字,就输出这n个数字。
本题我们只需要给这n+2个数字排序,求前n个数字和,看其是否等于后面两个数字的其中一个。
代码如下:
#include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<algorithm>
#include<string>
#include<string.h>
#include<random>
using namespace std;
typedef long long ll;
map<ll, ll>mp;
ll b[200003];
int main() {
int T;
cin >> T;
while (T--) {
mp.clear();
int n;
cin >> n;
for (int i = 0; i < n + 2; i++) {
cin >> b[i];
mp[b[i]]++;
}
sort(b, b + n + 2);
ll sum = 0;
for (int i = 0; i < n+1; i++) {
sum += b[i];
}
ll c = sum - b[n + 1];
mp[b[n + 1]]--;
if (mp[c]) {
mp[c]--;
for (auto i : mp) {
for (int j = 0; j < i.second; j++) {
cout << i.first << " ";
}
}
}
else if (b[n] == sum-b[n]) {
for (int i = 0; i < n; i++) {
cout << b[i] << " ";
}
}
else {
cout << -1;
}
cout << endl;
}
}