The main idea: give n numbers, the size of the first I number to meet 1 <= ai <= I, required to determine the sign of each number, so that the sum of all the numbers of 0
The idea of solving the problem: sum of 0, sum% 2 = = 0
Then analyze, because and for 0, so SUM/2 to be able to get through the n number, then enumerate to see if we can get
From the large to the small enumeration, take the current number of numbers and (sum/the current number) the value of the minimum value, and then sum minus the minimum value multiplied by the current value
If (sum/current this number) is the minimum value obtained, then in two cases
One can be removed, then (sum-minimum * current number) is exactly 0, that is, SUM/2 this number can be obtained, in line with the
One can not be removed, then (sum-minimum * current number) There will be left, because it is from the large to the small enumeration, so the remaining value can only see that the small value can be added to the
#include <cstdio>#include <algorithm>using namespace STD;#define MAXN 100010intNUM[MAXN], CNT[MAXN], CNT2[MAXN];intMain () {intN while(scanf("%d", &n) = =1) {Long Longsum =0;intMAX =0; for(inti =1; I <= N; i++) Cnt[i] = cnt2[i] =0; for(inti =1; I <= N; i++) {scanf("%d", &num[i]); Sum + = Num[i]; cnt[num[i]]++; max = max (max, num[i]); }if(Sum%2) {printf("no\n");Continue; }BOOLFlag =true; Sum/=2; for(inti = MAX; I >=1&& Flag; i--) {Cnt2[i] = min ((Long Long) Cnt[i], sum/i); Sum-= (Long Long) Cnt2[i] * i;if(Sum = =0) flag =false; }if(flag) {printf("no\n");Continue; }Else{printf("yes\n"); for(inti =1; I <= N; i++) {if(i = =1) {if(Cnt2[num[i]) >0) {printf("1"); cnt2[num[i]]--; }Else printf("-1"); }Else{if(Cnt2[num[i]) >0) {printf("1"); cnt2[num[i]]--; }Else printf("-1"); } }printf("\ n"); } }return 0;}
UVA-1614 Hell on the "Greed + reasoning"