1685: [Usaco2005 Oct]Allowance 津贴
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 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.
Input
Output
Sample Input
Sample Output
不错的贪心题
思路很简单,所有货币按面值排序,每次都尽量选择面值大的就好了,但是有很多细节要处理
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;
}