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.

作为对勤勤恳恳工作的贝茜的奖励,约翰已经决定开始支付贝茜一个小的每周津贴.   约翰有n(1≤N≤20)种币值的硬币,面值小的硬币总能整除面值较大的硬币.比如说,币值有如下几种:1美分,5美分,10美分,50美分…..
    利用给定的这些硬币,他将要每周付给贝茜一定金额的津贴C(1≤C≤10^8).
    请帮他计算出他最多能给贝茜发几周的津贴.

Input

    第1行:2个用空格隔开的整数n和C.
    第2到n+1行:每行两个整数表示一种币值的硬币.第一个整数V(I≤y≤10^8),表示币值.
第二个整数B(1≤B≤10^6),表示约翰拥有的这种硬币的个数.

Output

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

Sample Input

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;
}

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页