# bzoj 1685: [Usaco2005 Oct]Allowance 津贴（贪心）

## 1685: [Usaco2005 Oct]Allowance 津贴

Time Limit: 5 Sec   Memory Limit: 64 MB
Submit: 264   Solved: 195
[ Submit][ Status][ Discuss]

## Description

As a reward for record milk production, Farmer John has decided to start paying Bessie the cow a small weekly allowance. FJ has a set of coins in N (1 <= N <= 20) different denominations, where each denomination of coin evenly divides the next-larger denomination (e.g., 1 cent coins, 5 cent coins, 10 cent coins, and 50 cent coins). Using the given set of coins, he would like to pay Bessie at least some given amount of money C (1 <= C <= 100,000,000) every week. Please help him compute the maximum number of weeks he can pay Bessie.

利用给定的这些硬币，他将要每周付给贝茜一定金额的津贴C(1≤C≤10^8)．
请帮他计算出他最多能给贝茜发几周的津贴．

## Input

第1行：2个用空格隔开的整数n和C.
第2到n+1行：每行两个整数表示一种币值的硬币．第一个整数V(I≤y≤10^8)，表示币值．

## Output

一个整数，表示约翰付给贝茜津贴得最多的周数．

3 6
10 1
1 100
5 120

## Sample Output

111

use[i]表示当前周为了凑津贴第i种货币需要用use[i]个

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef struct Coin
{
int x, y;
bool operator < (const Coin &b) const
{
if(x>b.x)
return 1;
return 0;
}
}Coin;
Coin s[23];
int use[23];
int main(void)
{
int n, c, i, now, ans;
scanf("%d%d", &n, &c);
for(i=1;i<=n;i++)
scanf("%d%d", &s[i].x, &s[i].y);
sort(s+1, s+n+1);
ans = 0;
while(1)
{
now = c;
memset(use, 0, sizeof(use));
for(i=1;i<=n;i++)
{
if(now>s[i].x)
{
use[i] = min(now/s[i].x, s[i].y);
now -= s[i].x*use[i];
}
}
for(i=n;i>=1;i--)
{
if(now!=0 && use[i]+1<=s[i].y)
{
now = 0;
use[i]++;
break;
}
else if(now!=0)
{
now += use[i]*s[i].x;
use[i] = 0;
}
}
if(now!=0)
break;
for(i=1;i<=n;i++)
s[i].y -= use[i];
ans++;
}
printf("%d\n", ans);
return 0;
}

03-19 181

12-07 492
05-05 107
09-25 489
10-14 1515
01-20 218
07-17 426
10-14 1067
07-27 1323
07-13 1219
12-29 3220
12-08 322
07-23 310
07-23 625
07-03 507
04-21 370
02-23 429