题目描述
大概题意:
先给数组排序,本题总共就有两种情况,第一种:前n个数的和等于bn+1,x是bn+2,第二种:先算出前n+1项的和res,res减去bn+2,看看res-bn+2在前n+1项中出现过没,如果出现过,则x就是res-bn+2,和就是bn+2.否则就输出-1
别人的:
先排序,有两种可能,一是第二大的数字是和,最大的数字是x,这种情况只需要将前n个数字相加看是否等于第n+1个数字即可,否则看第二种情况最大的数字是数字和,这种情况需要将前n+1个数字相加得到和减去最大的数字,得到x,如果序列种存在这个x则该情况成立,如果两种情况都不满足,则输出-1
————————————————
版权声明:本文为CSDN博主「bonely」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sjjzslhhh/article/details/115598104
样例
Example
Input
Copy
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
Copy
2 3 7
-1
2 2 2 3 9
1 2 6
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
int t;
int n,x;
long long b[200010];
map<long long,int>::iterator it;
int main(int argc, char** argv) {
scanf("%d",&t);
while(t--){
int flag=1;
map<long long,int> q;
long long res=0;
scanf("%d",&n);
for(int i=1;i<=n+2;i++){
scanf("%lld",&b[i]);
q[b[i]]++;
}
sort(b+1,b+n+3);
for(int i=1;i<=n;i++){
res+=b[i];
}
if(res==b[n+1]){
flag=0;
for(int i=1;i<=n;i++){
cout<<b[i]<<" ";
}
cout<<endl;
}else{
res+=b[n+1];
long long c=res-b[n+2];
q[b[n+2]]--;
if(q[c]){
flag=0;
q[c]--;
for(it=q.begin();it!=q.end();it++){
for(int i=0;i<it->second;i++){
cout<<it->first<<" ";
}
}
cout<<endl;
}else{
cout<<"-1"<<endl;
}
}
}
return 0;
}