集训第四周(高效算法设计)H题 (贪心)
Description
Input
The input file contains several test cases, each of them as described below. The first line of the input contains the single integer number n<tex2html_verbatim_mark>( 1n100 000<tex2html_verbatim_mark> ). The second line of the input contains n<tex2html_verbatim_mark> integer numbers -- ai<tex2html_verbatim_mark> ( 1aii<tex2html_verbatim_mark> ).Output
For each test case, the first line of the output must contain `` Yes'' if the trading session with specified volumes is possible and `` No'' otherwise. In the former option a second line must contain n<tex2html_verbatim_mark> numbers -- bi<tex2html_verbatim_mark> .Sample Input
4 1 2 3 3 4 1 2 3 4
Sample Output
No Yes 1 -1 -1 1
给你一个数组,选择其中的一半的数变成负数,然后这个数组的和为0
首先来判断一下NO的情况,如果这个数组的和是一个奇数的话,那么你无论怎么选都不会有结果,然后如果你数组长度为1,那么就没得选了。
YES该怎么做?先从大到小排序,然后一路加下去(使和大于数组和一半的不能加),如果刚好和等于数组和的一半,那么你选择的那些数就是应该乘以-1的数了
#include"iostream"
#include"algorithm"
using namespace std;
const int maxn=100000+10;
int f[maxn];
long long sum;
int n;
struct node
{
int val,num;
}a[maxn];
bool cmp(struct node a1,struct node a2)
{
return a1.val>a2.val;
}
bool Init()
{
sum=0;
for(int i=0;i<n;i++)
{
cin>>a[i].val;
a[i].num=i;
f[i]=-1;
sum+=a[i].val;
}
if(sum%2) return false;
else return true;
}
void Work()
{
sort(a,a+n,cmp);
long long cur=0;
for(int i=0;i<n;i++)
{
if(cur+a[i].val<=sum/2)
{
cur+=a[i].val;
f[a[i].num]=1;
if(cur==sum/2) break;
}
}
}
void print()
{
for(int i=0;i<n-1;i++)
{
if(i!=0) cout<<' ';
cout<<f[i];
}
cout<<' '<<f[n-1]<<endl;
}
int main()
{
while(cin>>n)
{
if(!Init()||n==1) cout<<"No"<<endl;
else
{
cout<<"Yes"<<endl;
Work();
print();
}
}
return 0;
}