A. Make it Beautiful

 原题

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;
 }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值