A. Ehab Fails to Be Thanos
题目链接: http://codeforces.com/contest/1174/problem/A
题目
- 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
题意
给你一个长度为 2n 的数组 a, 对其重新排序, 使得前 n 和后 n 元素的总和不想等, 若无法排序输出 - 1, 可以则输出重新排序过的长度为 2n 的数组 a.
思路
先判断 a 数组中的数是否一样, 若一样则无法排序使其前 n 个数之和不为后 n 个数之和, 输出 - 1;
否则, 求出前 n 与后 n 个数的和, 判断他们是否相等, 如果相等, 将 a 排序再顺序输出即可, 不想等则直接将原来 a 数组反向输出即可.
- //
- // 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;
- }
来源: http://www.bubuko.com/infodetail-3086052.html