A. Ehab Fails to Be Thanos
Topic links: http://codeforces.com/contest/1174/problem/A
topic
You’re given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn’t equal to the sum of the last n elements?
Input
The first line contains an integer n(1≤n≤1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a1, a2, …, a2n (1≤ai≤106) — the elements of the array a
Output
If there’s no solution, print “-1” (without quotes). Otherwise, print a single line containing 2n
space-separated integers. They must form a reordering of a. You are allowed to not change the order
The meaning of problems
2n give you an array of length a, reorder, and after the first n such that the sum of n elements not want to wait, -1 if not sort output, the output can be re-sorted array of length 2n a.
Thinking
First determine whether a number of the same array, if the same can not be sorted before the number n and it is not the sum of the number n, the output 1;
Otherwise, after obtaining the first n and the number n, it is determined whether they are equal, if they are equal, then the order of the output to a sort, and the like directly do not want the original inverted output to a array.
// // Created by hjy on 19-6-4. // #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=2e5+10; int main() { int n; while(cin>>n) { int a[maxn]; int x; cin>>x; a[0]=x; int flag=a[0]; int num=1; for(int i=1;i<2*n;i++) { cin>>a[i]; if(a[i]==flag) num++; } if(num==2*n) cout<<"-1"<<endl; else { int l=accumulate(a,a+n,0); cerr<<"l="<<l<<endl;//cerr对提交无影响 int r=accumulate(a+n,a+2*n,0); cerr<<"r="<<r<<endl; if(l!=r) { for(int i=0;i<2*n;i++) { cout<<a[i]<<' '; } cout<<endl; } else { sort(a,a+2*n); for(int i=0;i<2*n;i++) cout<<a[i]<<' '; cout<<endl; } } } return 0; }