- Source Code
#include<iostream> #include<algorithm> #include<string.h> using namespace std; struct node { int x, y; }cow[21]; bool cmp(node a, node b) { return a.x < b.x; } int main() { int N, C; while (cin >> N >> C) { int sum = 0, used[21]; memset(used, 0, sizeof(used)); int i, j; for (i = 0, j = 0; i < N; i++) { int a, b; cin >> a >> b; if (a >= C) sum = sum + b; else { cow[j].x = a; cow[j++].y = b; } } sort(cow, cow + j, cmp); while (1) { int s = C,temp=0; for (i = j - 1; i >= 0; i--) { if (s > 0&&cow[i].y>0) { temp = min(cow[i].y, s / cow[i].x); used[i] += temp; s = s - temp*cow[i].x; } } if (s > 0) { for (i = 0; i < j; i++) { if (used[i] < cow[i].y&&cow[i].x>=s) { used[i]++; s = s - cow[i].x; break; } } } if (s > 0) break; sum++; for (i = 0; i < j; i++) { cow[i].y -= used[i]; used[i] = 0; } } cout << sum << endl; } return 0; }
POJ 3040 Allowance(贪心经典题目)
最新推荐文章于 2023-11-02 16:25:25 发布