ZeptoLab Code Rush 2015 C题Om Nom and Candies

来源:互联网 发布:雅思单词书推荐 知乎 编辑:程序博客网 时间:2024/05/17 08:11

C. Om Nom and Candies
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

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.

Sample test(s)
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.



题意大概就是一个怪兽吃糖果,有两种糖果,然后每种糖果都有其重量和幸福感(吃完后得到的幸福感),然后这个怪兽要求在重量不超过C的情况下使幸福感最大,糖果数量没有限制,问最大的幸福感是多少。我的做法就是先吃性价比最高的糖。看还剩下多少重量可以吃另外一种糖。如果重量还有剩余,然后逐步减少性价比最高的糖的数量,去吃另一种糖,直到重量剩余为0或者性价比最高的糖的数量为0.就KO了。

#include<limits>#include<queue>#include<vector>#include<list>#include<map>#include<set>#include<deque>#include<stack>#include<bitset>#include<algorithm>#include<functional>#include<numeric>#include<utility>#include<sstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cmath>#include<cstdlib>#include<cstring>#include<ctime>#define LL long long#define eps 1e-10#define pi acos(-1)#define INF 0x7fffffff#define delta 0.98 //模拟退火递增变量using namespace std;int dcmp(double x){if (fabs(x)<=eps) return 0;if (x>0) return 1;return -1;}int main(){LL c,hr,hb,wr,wb;scanf("%I64d%I64d%I64d%I64d%I64d",&c,&hr,&hb,&wr,&wb);if (dcmp(hb*1.0/wb-hr*1.0/wr)<0){ //保证蓝色是最大的 swap(hr,hb);swap(wr,wb);}LL l=c;LL k=l/wb;LL ans=k*hb;l=c-k*wb;if (l>=wr){ans+=(l/wr)*hr;l-=l/wr*wr;}if (hr<=hb && wr>=wb)cout<<ans<<endl;else{LL gg=ans;while (k>0 && l){k--;l+=wb;gg-=hb;if (l>=wr){gg+=l/wr*hr;l-=l/wr*wr;}if (gg>ans) ans=gg;}cout<<ans<<endl;}return 0;}


0 0
原创粉丝点击