Poj 3040 Allowance【贪心模拟】

来源:互联网 发布:雷蛇2000 mac驱动 编辑:程序博客网 时间:2024/05/20 06:53

Allowance
Time Limit: 1000MS Memory Limit: 65536KTotal 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 610 11 1005 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