首页 > 代码库 > UVa1614 Hell on the Markets (贪心,结论)

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 (贪心,结论)