Allowance
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1554 | Accepted: 644 |
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 ompute the maximum number of weeks he can pay Bessie.
Input
* Line 1: Two space-separated integers: N and C
* Lines 2..N+1: Each line corresponds to a denomination of coin and contains two integers: the value V (1 <= V <= 100,000,000) of the denomination, and the number of coins B (1 <= B <= 1,000,000) of this denomation in Farmer John's possession.
* Lines 2..N+1: Each line corresponds to a denomination of coin and contains two integers: the value V (1 <= V <= 100,000,000) of the denomination, and the number of coins B (1 <= B <= 1,000,000) of this denomation in Farmer John's possession.
Output
* Line 1: A single integer that is the number of weeks Farmer John can pay Bessie at least C allowance
Sample Input
3 6 10 1 1 100 5 120
Sample Output
111
Hint
INPUT DETAILS:
FJ would like to pay Bessie 6 cents per week. He has 100 1-cent coins,120 5-cent coins, and 1 10-cent coin.
OUTPUT DETAILS:
FJ can overpay Bessie with the one 10-cent coin for 1 week, then pay Bessie two 5-cent coins for 10 weeks and then pay Bessie one 1-cent coin and one 5-cent coin for 100 weeks.
FJ would like to pay Bessie 6 cents per week. He has 100 1-cent coins,120 5-cent coins, and 1 10-cent coin.
OUTPUT DETAILS:
FJ can overpay Bessie with the one 10-cent coin for 1 week, then pay Bessie two 5-cent coins for 10 weeks and then pay Bessie one 1-cent coin and one 5-cent coin for 100 weeks.
题意:组合金币,使得每个星期都有不少于C的奖励,问最多能组合多少个星期。
思路:如果恰好能组成C,那么就是尽可能从大往小取,如果不能组成C的话,就在之前的基础上,从小往大取。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int val,num;
}coin[25];
bool cmp(node a,node b)
{
return a.val<b.val;
}
int n,c,num[25],ans;
bool solve()
{
int i,j,k,ret=0;
for(i=n;i>=1;i--)
if(c-ret>=coin[i].val)
{
k=min((c-ret)/coin[i].val,coin[i].num);
num[i]+=k;
ret+=coin[i].val*k;
}
if(ret==c)
return true;
for(i=1;i<=n;i++)
if(ret<c)
{
k=min((c-ret)/coin[i].val+1,coin[i].num-num[i]);
num[i]+=k;
ret+=coin[i].val*k;
}
if(ret<c)
return false;
else
return true;
}
int main()
{
int i,j,k,maxn;
while(~scanf("%d%d",&n,&c))
{
for(i=1;i<=n;i++)
scanf("%d%d",&coin[i].val,&coin[i].num);
sort(coin+1,coin+1+n,cmp);
while(true)
{
memset(num,0,sizeof(num));
if(!solve())
break;
maxn=1e9;
for(i=1;i<=n;i++)
if(num[i]>0)
maxn=min(maxn,coin[i].num/num[i]);
ans+=maxn;
for(i=1;i<=n;i++)
coin[i].num-=maxn*num[i];
}
printf("%d\n",ans);
}
}