Poj 3040 Allowance【贪心模拟】

Allowance
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2542 Accepted: 1038

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.

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.

题意:

有个人想给他的奶牛发工资,每周都发金额至少为c的钱,现在他有一些面值的硬币,给出对应的数量,而且面值之间呈倍数关系,问这个人的钱最多能发多少周。


题解:

最开始的理解是,如果刚好可以凑成面值C,那么肯定这样的方式是最优的解法,但是就好像成了动态一直更新讨论的感觉了,好复杂......

后来发现,原来面值之间有倍数关系,情况就相对简单了,因为大的面值能由小面值拼凑出来,因此完全刻意优先使用大的面值的,但是处理起来还是很复杂........

后来参考了大神的思路,发现自己思维还是没打开.......

其实贪心的策略是一样的,但是大神使用了一个辅助数组来存放每次某个物品需要选多少,并且在动态更新选举的个数,自己咋就没想到呢!!!

后来研读了很久,终于敢下手写总结了.....


贪心策略:

1,从面值最大的开始选取,如果超过了目标值,先不选(其实可以先特判累加这些超出的个数,算是一种优化吧)

2,预选一些小于目标值的硬币,使得选取的面值小于目标值,这时候的值但是肯定是小于目标值而且是非常接近目标值的

3,从面值最小的开始选取,选择使得当前值大于目标值的最小面值的那个硬币

4,如果当前值无法大于目标值,说明无法满足条件,结束执行,否则使用当前策略直接发去一些周数的工资,并且更新数据

5,重复执行,直至第4条结束执行。



/*
http://blog.csdn.net/liuke19950717
*/
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
struct node
{
	ll val,num;
}x[30];
int cmp(node a,node b)
{
	return a.val<b.val;
}
ll cal(ll n,ll c)
{
	ll num[30]={0},sum=c;//数组存放每个元素取多少个 
	for(ll i=n-1;i>=0&∑--i)//尝试进行选取 
	{
		ll tp=min(sum/x[i].val,x[i].num);//某一个最多能选多少 
		num[i]=tp;//记录数量 
		sum-=tp*x[i].val;//需求量减小 
	}
	if(sum>0)//如果没能正好凑够 
	{
		for(ll i=0;i<n;++i)//从最小的遍历 
		{
			if(x[i].num>num[i]&&x[i].val>=sum)//选还剩下的,能满足需求的 
			{
				++num[i];//选上 
				sum=0;//选好了 
				break;
			}
		}
	}
	if(sum>0)//说明无法满足 
	{
		return 0;
	}
	ll ans=0x3f3f3f3f;//初始化一个无穷大 
	for(ll i=0;i<n;++i)
	{
		if(num[i])//找到当前策略能发的周数
		{
			ans=min(ans,x[i].num/num[i]);
		}
	}
	for(ll i=0;i<n;++i)
	{
		x[i].num-=ans*num[i];//减去发去的工资 
	}
	return ans;
}
int main()
{
	ll n,c;
	while(~scanf("%lld%lld",&n,&c))
	{
		for(ll i=0;i<n;++i)
		{
			scanf("%lld%lld",&x[i].val,&x[i].num);
		}
		sort(x,x+n,cmp);
		ll num=cal(n,c),ans=0;
		while(num)//执行到无法满足
		{
			ans+=num;
			num=cal(n,c);
		}
		printf("%lld\n",ans);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值