标签:
Description InputThe 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> ).OutputFor 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 Input4 1 2 3 3 4 1 2 3 4 Sample OutputNo Yes 1 -1 -1 1 一组数据,选择其中的一半的数变成负数,加上另一半可以和为0 |
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; long long sum; int t; int f[100000000]; struct node { int zg; int id; }; node a[8000010]; int cmp(node x,node y) { return x.zg>y.zg; } int main() { while(cin>>t) { long long sum=0; for(int i=0; i<t; i++) { cin>>a[i].zg; a[i].id=i; f[i]=-1; sum+=a[i].zg; } if(sum%2||t==1) cout<<"No"<<endl; else { cout<<"Yes"<<endl; sort(a,a+t,cmp); int s=0; for(int i=0; i<t; i++) { if(s+a[i].zg<=sum/2) { s+=a[i].zg; f[a[i].id]=1; if(s==sum/2) break; } } cout<<f[0]; for(int i=1; i<t; i++) { cout<<‘ ‘<<f[i]; } cout<<endl; } } return 0; }
UVA - 1614 Hell on the Market(贪心)
标签:
原文地址:http://www.cnblogs.com/hfc-xx/p/4709041.html