UVa1614 Hell on the Markets (贪心,结论)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVa1614 Hell on the Markets (贪心,结论)相关的知识,希望对你有一定的参考价值。
链接: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 (贪心,结论)的主要内容,如果未能解决你的问题,请参考以下文章
uva 1614Hell on the Markets(算法效率--贪心)