ZeptoLab Code Rush 2015 C题Om Nom and Candies

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;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值