Codeforces 526C - Om Nom and Candies
2016-05-10 19:37
543 查看
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs
Wr grams and each blue candy weighs
Wb grams. Eating a single red candy gives Om Nom
Hr joy units and eating a single blue candy gives Om Nom
Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than
C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type
he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
Input
The single line contains five integers C, Hr, Hb, Wr, Wb
(1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Output
Print a single integer — the maximum number of joy units that Om Nom can get.
Examples
Input
Output
Note
In the sample test Om Nom can eat two candies of each type and thus get
16 joy units.
题意:无限背包问题,商品只有两种,一开始以为可以三分做。。然后疯狂wa。
分析:暴力枚举两种商品的数量,当max(wa,wb) < sqrt(c)时,wa的糖b可以换做wb的糖a,调整一下解一定更优,因此最多枚举到sqrt(c),
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs
Wr grams and each blue candy weighs
Wb grams. Eating a single red candy gives Om Nom
Hr joy units and eating a single blue candy gives Om Nom
Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than
C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type
he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
Input
The single line contains five integers C, Hr, Hb, Wr, Wb
(1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Output
Print a single integer — the maximum number of joy units that Om Nom can get.
Examples
Input
10 3 5 2 3
Output
16
Note
In the sample test Om Nom can eat two candies of each type and thus get
16 joy units.
题意:无限背包问题,商品只有两种,一开始以为可以三分做。。然后疯狂wa。
分析:暴力枚举两种商品的数量,当max(wa,wb) < sqrt(c)时,wa的糖b可以换做wb的糖a,调整一下解一定更优,因此最多枚举到sqrt(c),
#include <cstdio> #include <iostream> using namespace std; long long c,hr,hb,wr,wb,ans; long long check(long long x) { return x*hr + (c-x*wr)/wb*hb; } int main() { cin.sync_with_stdio(false); cin>>c>>hr>>hb>>wr>>wb; for(int i = 0;i <= min(c/wr,100000ll);i++) ans = max(ans,check(i)); swap(hr,hb),swap(wr,wb); for(int i = 0;i <= min(c/wr,100000ll);i++) ans = max(ans,check(i)); cout<<ans<<endl; }
相关文章推荐
- JDBC / Spring / MyBatis 性能比较
- 让Myeclipse10支持Mac OSX – Retina显示屏
- COM开发-CComPtr用法
- HDU 3760(最短路思路)
- tr命令
- html 弹窗
- 自定义View----Banner广告条
- css半透明
- Python学习日记.1
- FTP服务
- C++:public继承、protected 继承、private继承
- VM参数收集(部分)
- 使用开源软件的原因
- JS对象添加新的字段
- 通过进度条控制图片的透明度
- SVN 使用
- JS限定手机版中图片大小随分辨率自动调整
- 初创团队管理
- flashcache之创建
- android学习之基于百度地图的地点检索和标记