1685: [Usaco2005 oct]allowance allowance time Limit:5 Sec Memory MB
submit:193 solved:141
[Submit] [Status] [Discuss] Description
As a reward for record milk production, farmer John has decided to start paying Bessie the cow a small weekly. FJ has a set of coins in N (1 <= N <=) different denominations, where each denomination of coin evenly divides th e Next-larger denomination (e.g., 1 cent coins, 5 cent coins, cent coins, and cent). Using the given set of coins, he would like to pay Bessie at least some given amount of money C (1 <= C <= 100,000,0 ) every week. Please help him compute the maximum number of weeks he can pay Bessie. As an award to Bessie, who worked diligently, John had decided to start paying Bessie a small weekly allowance. John has N (1≤n≤20)-denominated coins, with small denominations that can always divide the larger coins. For example, there are several currencies: 1 cents, 5 cents, 10 cents, 50 cents ... using the given coins, he will pay Betsy a certain amount of allowance C (1≤C≤10^8) every week. help him figure out how much he can give Betsy a few weeks ' allowance. Input line 1th: 2 integers n and c separated by spaces. 2nd to N+1 line: Two integers per line represents a coin of a currency value. The first integer, V (i≤y≤10^8), represents the value of the currency. The second integer, B (1≤b≤10^6), represents the number of such coins that John owns. Output An integer representing the maximum number of weeks that John paid Bessie. Sample Input 3 6
10 1
1 1 00
5 1 20
Sample Output 111
Sample Description
John wants to pay Betsy 6 cents a week. He has a 10-cent coin, 100 1-cent coins and 120 5-cent coins. John could pay Betsy a 10-cent coin in the first week, then 10 weeks a week to 2 5 cents a month, and a 100-cent penny and a 1-cent penny a week for the next 5 weeks. Total 111 weeks. HINT
Source
Obviously, the less money you spend every week on this topic, the better.
Because the front of the coin can always divide the back, so arrange a sequence from big to small to find the best solution ...
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace
Std
const int MAXN = 30;
struct c{int v,b;
BOOL operator < (const C &b) Const {return v < B.V;
}}COIN[MAXN];
int N,I,J,C,H[MAXN];
Long ans = 0;
int main () {#ifdef Yzy freopen ("Yzy.txt", "R", stdin);
#endif cin >> n >> C;
for (i = 1; I <= n; i++) scanf ("%d%d", &coin[i].v,&coin[i].b);
Sort (coin + 1,coin + n + 1);
while (1) {int rest = C;
memset (h,0,sizeof (h));
for (i = n; i > 0; i--) {int temp = min (COIN[I].B,REST/COIN[I].V);
Rest-= temp * COIN[I].V;
H[i] = temp;
} if (Rest > 0) for (i = 1; I <= n; i++) {if (rest <= 0) break;
if (coin[i].b > H[i]) {++h[i];
Rest-= COIN[I].V;
} if (rest <= 0) {int tot = 1E9;
for (i = 1; I <= n; i++) if (H[i] > 0) tot = min (tot,coin[i].b/h[i));
Ans + + 1ll*tot; For(i = 1; I <= n; i++) if (H[i] > 0) coin[i].b-= tot * H[i];
else break;
cout << ans;
return 0; }