原题
An array aa is called ugly if it contains at least one element which is equal to the sum of all elements before it. If the array is not ugly, it is beautiful.
For example:
- the array [6,3,9,6][6,3,9,6] is ugly: the element 99 is equal to 6+36+3;
- the array [5,5,7][5,5,7] is ugly: the element 55 (the second one) is equal to 55;
- the array [8,4,10,14][8,4,10,14] is beautiful: 8≠08≠0, 4≠84≠8, 10≠8+410≠8+4, 14≠8+4+1014≠8+4+10, so there is no element which is equal to the sum of all elements before it.
You are given an array aa such that 1≤a1≤a2≤⋯≤an≤1001≤a1≤a2≤⋯≤an≤100. You have to reorder the elements of aa in such a way that the resulting array is beautiful. Note that you are not allowed to insert new elements or erase existing ones, you can only change the order of elements of aa. You are allowed to keep the array aa unchanged, if it is beautiful.
题目大意:
排列给定的数列,让其第n个数不等于前n-1个数的总和
解决思路:
被好友提醒到了这个神奇的思路,是我没有想到了
可以直接一大一小给定排列
除非是全程同一个输字否则就是可以的
代码:
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n;
cin>>n;
int a[55];
int b[55];
int num=1;
a[0]=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
b[i]=a[i];
if(a[i]==a[i-1]) num++;
}
if(num==n)
{
cout << "NO" << endl;
return ;
}
sort(a+1,a+n+1);//小
sort(b+1,b+n+1,greater<int>());//大
cout<<"YES"<<endl;
if(n%2==0)//偶数情况
{
for(int i=1;i<=n/2;i++)
cout<<b[i]<<" "<<a[i]<<" ";
cout<<endl;
}
else//奇数
{
for(int i=1;i<=n/2;i++)
cout<<b[i]<<" "<<a[i]<<" ";
cout<<a[n/2+1]<<endl;//x/n的这个情况是向下取整的需要加一
}
}
int main()
{
int t;
cin>>t;
while(t--) solve();
return 0;
}