UVA-1614 Hell on the "Greed + reasoning"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.