目录
1.Problem
You're given an array aa of length 2n2n. Is it possible to reorder it in such way so that the sum of the first nn elements isn't equal to the sum of the last nn elements?
You're given an array aa of length 2n2n. Is it possible to reorder it in such way so that the sum of the first nn elements isn't equal to the sum of the last nn elements?
2.Input
The first line contains an integer nn (1≤n≤10001≤n≤1000), where 2n2n is the number of elements in the array aa.
The second line contains 2n2n space-separated integers a1a1, a2a2, ……, a2na2n (1≤ai≤1061≤ai≤106) — the elements of the array aa.
3.Output
If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n2n space-separated integers. They must form a reordering of aa. You are allowed to not change the order.
4.Examples
4.1input
3
1 2 2 1 3 1
4.2output
2 1 3 1 1 2
5.Code
#include<bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
int readint(){
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n;
int a[2005];
int main(){
n=readint();
for(int i=1;i<=2*n;i++) a[i]=readint();
sort(a+1,a+2*n+1);
if(a[1]==a[2*n]) cout<<-1<<endl;
else{
for(int i=1;i<=2*n;i++) cout<<a[i]<<' ';
cout<<endl;
}
return 0;
}
6.Conclusion
这段C++代码的主要目的是读取一个整数n,然后读取2n个整数,并对这2n个整数进行排序。最后,如果排序后的数组中的最小值和最大值相等,输出-1;否则,输出排序后的整数数组。
以下是代码的主要逻辑:
通过
readint
函数读取输入的整数n
。定义了一个整数数组
a
,用于存储后续输入的2n个整数。使用循环读取2n个整数,并将其存储在数组
a
中。使用
sort
函数对数组a
进行升序排序。检查排序后的数组中的第一个元素和最后一个元素是否相等。如果相等,输出-1,表示无法满足要求。否则,输出排序后的整数数组。
总体来说,这段代码的功能是读取一系列整数,排序后检查是否有相同的最小和最大值,然后输出相应的结果。