Poj 3040 Allowance【贪心模拟】
2016-03-23 18:16
337 查看
Allowance
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
Sample Output
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条结束执行。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2542 | Accepted: 1038 |
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; }
相关文章推荐
- IntelliJ IDEA中怎么全局搜索替换(整个项目)(Eclipse)
- Qt入门之信号与槽机制
- iOS app 打包上传 问题 ERROR-ITMS 90060(the bundle is invalid .the value for ker CFBundleShortVersionStrin)
- ios中audio session纪录
- Volley网络框架的使用
- MVC route 和 Angular router 单页面的一些方式
- 二进制、十进制、十六进制相互转换
- MySQL触发器更新本表数据异常:Can't update table 'tbl' in stored function/trigger because it
- Android中App可分配内存的大小
- windows下安装Apache(欢迎留言)
- 杭电1787GCD Again(欧拉函数)
- SQLite Expert查看float数据
- win7下qt error: undefined reference to `_imp__getnameinfo@28'解决
- uvc camera MTK平台调试总结
- VMware 安装 CentOS 安装 CentOS-6.5-i386-minimal.iso 版本
- 第四周项目1-求最大公约数(1)
- HDU 2175 汉诺塔IX
- poj 1050 To the Max(最大子矩阵和)
- linux中socket的理解
- extjs