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 ( 1



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 numbers -- bi .Sample Input
4 1 2 3 3 4 1 2 3 4
Sample Output
No Yes 1 -1 -1 1
题意: 给定一个长度为n的序列a,满足1<=ai<=i(很关键的条件),要求确定每个数的正负号,使得最后的和为0.没有这样的策略则输出No。
思路:贪心。因为1<=ai<=i,那么很容易得到的一个结论就是当所有数的绝对值得和为奇数时,一定没有方法使最后的和为0.当为偶数时,一定会存在一种方法。那么选取的时候必定是正1为sum的一半,-1为sum的一半,在遍历时不断贪心找到为+1的那一半即可。
code:#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <sstream> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; const int INF=0x3fffffff; const int inf=-INF; const int N=100005; const int M=20005; const int mod=1000000007; const double esp=1e-4; const double pi=acos(-1.0); #define cls(x,c) memset(x,c,sizeof(x)) #define cpy(x,a) memcpy(x,a,sizeof(a)) #define fr(i,s,n) for (int i=s;i<=n;i++) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lrt rt<<1 #define rrt rt<<1|1 #define middle int m=(r+l)>>1 #define lowbit(x) (x&-x) #define pii pair<int,int> #define mk make_pair #define IN freopen("in.txt","r",stdin); #define OUT freopen("out.txt","w",stdout); int v[N],s[N]; int main() { int n; while (~scanf("%d",&n)&&n) { ll sum=0; fr(i,1,n) scanf("%d",&v[i]),sum+=v[i]; if (sum&1) {puts("No");continue;} ll p=0; for (int i=n;i>0;i--) { if ((p+v[i])*2<=sum)p+=v[i],s[i]=1; //所有的为+1的那一半 else s[i]=-1; } puts("Yes"); fr(i,1,n) printf("%d ",s[i]); puts(""); } }