码迷,mamicode.com
首页 > 其他好文 > 详细

UVa1614 Hell on the Markets (贪心,结论)

时间:2016-09-22 23:37:44      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

链接:http://bak.vjudge.net/problem/UVA-1614

分析:http://blog.csdn.net/wcr1996/article/details/43957461

有了这个结论,接下来就是贪心搞一搞了。

 

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 const int maxn = 100000 + 5;
 7 
 8 int n;
 9 pair<int, int> a[maxn];
10 int sign[maxn];
11 
12 int main() {
13     while (scanf("%d", &n) == 1) {
14         long long sum = 0;
15         for (int i = 0; i < n; i++) {
16             int x; scanf("%d", &x); sum += x;
17             a[i] = make_pair(x, i);
18         }
19         if (sum & 1) { printf("No\n"); continue; }
20         sort(a, a + n);
21         memset(sign, -1, sizeof(sign));
22         sum /= 2;
23         for (int i = n - 1; i >= 0; i--)
24             if (a[i].first <= sum) { sum -= a[i].first; sign[a[i].second] = 1; }
25             else if (sum == 0) break;
26         printf("Yes\n");
27         for (int i = 0; i < n; i++) printf("%d%c", sign[i], i == n - 1 ? \n:  );
28     }
29     return 0;
30 }

 

UVa1614 Hell on the Markets (贪心,结论)

标签:

原文地址:http://www.cnblogs.com/XieWeida/p/5898244.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!