从大到小排序,如果当前和小于零则加,大于零则减,判断最后是否收敛于零。
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=110000;
int n,v[N];
struct node
{
int i;
ll num;
}a[N];
bool cmp(node a,node b)
{
return a.num<b.num;
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++) scanf("%lld",&a[i].num),a[i].i=i;
sort(a,a+n,cmp);
ll sum=0;
for(int i=n-1;i>=0;i--)
{
if(sum<=0)
{
sum+=a[i].num;
v[a[i].i]=1;
}
else
{
sum-=a[i].num;
v[a[i].i]=-1;
}
}
if(sum==0)
{
printf("Yes\n");
for(int i=0;i<n-1;i++) printf("%d ",v[i]);
printf("%d\n",v[n-1]);
}
else
{
printf("No\n");
}
}
}