C. Om Nom and Candies(ZeptoLab Code Rush 2015)
来源:互联网 发布:南京中兴软件测试 编辑:程序博客网 时间:2024/05/19 05:33
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 Cgrams 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.
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Print a single integer — the maximum number of joy units that Om Nom can get.
10 3 5 2 3
16
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
题意:
给出可吃最大糖果重量,和两种糖果的单个重量和单个快乐值,求其获得的最大快乐值。
题解:
由于数据范围比较大,暴力枚举最坏情况下会超时,假如第一个的wr*wr>=c,那么肯定可以在sqrt(c)内枚举完,假如wr*wr<c,此时如果wb*wb>=c,那么就枚举wb,假如wb*wb也<c,那么我们就比较两个的单位克的权值,权值小的个数肯定不会大于sqrt(c)个。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;int main(){ long long c,hr,hb,wr,wb; while(cin>>c>>hr>>hb>>wr>>wb) { if(wr*wr<c&&((1.0*hr/wr>1.0*hb/wb)||wb*wb>=c)) { swap(wr,wb); swap(hr,hb); } long long ans=0; for(long long i=0;i*i<=c&&wr*i<=c;i++) { ans=max(ans,hr*i+(c-wr*i)/wb*hb); } cout<<ans<<endl; } return 0;}
- ZeptoLab Code Rush 2015 C. Om Nom and Candies
- ZeptoLab Code Rush 2015---C. Om Nom and Candies
- ZeptoLab Code Rush 2015 C题Om Nom and Candies
- C. Om Nom and Candies(ZeptoLab Code Rush 2015)
- ZeptoLab Code Rush 2015 B. Om Nom and Dark Park
- ZeptoLab Code Rush 2015 B. Om Nom and Dark Park
- ZeptoLab Code Rush 2015_B - Om Nom and Dark Park
- Codeforces ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS
- 526C - Om Nom and Candies
- CF 526C Om Nom and Candies
- Codeforces 526C Om Nom and Candies
- Codeforces 526C - Om Nom and Candies
- CF Zepto Code Rush 2014 B. Om Nom and Spiders
- ZeptoLab Code Rush 2015
- CF Om Nom and Candies
- ZeptoLab Code Rush 2015 -- (A,B,C)
- Codeforces 526C - Om Nom and Candies (贪心 + 思维)
- codeforces 526 c Om Nom and Candies 剩余系
- svnync svn同步
- (一)等价类划分法
- AWTUtilities
- 二叉树 深度优先搜索(DFS)、广度优先搜索(BFS)
- Brown film faced plywood the worthy formply
- C. Om Nom and Candies(ZeptoLab Code Rush 2015)
- android(studio)之旅-(暂时留坑...)多activity的创建及其交互
- Eclipse 导入Hadoop 2.6.0 源码
- SmartTabLayout-master Demo设计浅谈
- 处女真那么重要吗?
- 灵动的别墅空间 抒发朴实的自然之美
- 【Android】深入理解Android中的自定义属性
- MYSQL时间函数
- MFC控件位置随窗体大小而改变