[ZeptoLab Code Rush 2015]Om Nom and Candies

Description

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 ≤ 10e9).

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.

Standard Report

If there is a kind of candy which weighs greater than C , then we can iterate over the number of it to buy, which is less than C .
Otherwise, without loss of generality we suppose HbWb < HrWr . If the number of the blue candies that Om Nom eats is more than Wr, he could eat Wb red candies instead of Wr blue candies, because Hb × Wr < Wb × Hr. It means the number of the blue candies will be less than C , and we can iterate over this number.

Quote Report

Author: tokers
Original Link: http://blog.csdn.net/guard_mine/article/details/44887395

My Problem Report

我第一眼看到这道题觉得是线性规划,但因为题目中数据规模太大,所以很快就排除了暴力枚举。接下来想到的是用完全背包,但再深入分析复杂度,发现丝毫没有减少,于是陷入了困境。在后续的思考中我甚至想尝试用线性规划中的高级数学方法求解,但最后都以失败告终。
读完题解报告后,我惊讶地发现那种思维方式居然和我们学习背包问题之前的解题思维非常相似。题解中提到的性价比实质上是一种贪心算法。对这一类问题使用贪心算法最难解决的是使我们的决策保持无后效性,接下来我们来看看引用题解中的算法是怎样来回避这种后效性影响的。
引用题解中将问题按糖果的花费将问题划分为两类,重点论述第二类情况。这部分推导最让人难以理解的是把k⋅Wr的那一部分蓝色的糖果拿来换成红色的糖果,下面我来补充证明一下这个推导过程:

保持引用题解中的假设不发生改变

同上X=Y+k•Wr,此时k•Wr部分的花费是k•Wr•Wb,价值是k•Wr•Hb
现在我们开始进行替换,这里所指的替换并不是将k•Wr的蓝色糖果替换为k•Wr的红色糖果,而是在保持花费(k•Wr•Wb)不变的情况下进行替换,也就是说我们将k•Wr的蓝色糖果替换为k•Wb的红色糖果。这样我们就保证了在无后效性的条件下转换决策。
再来看现在状态(价值/H)发生的改变,替换后的价值为k•Wb•Hr,根据条件约束,k•Wb•Hr k•Wr•Hb,因此我所做的决策(替换)是更优的。

当Wb•Hr < Wr•Hb时同理可证。当Wb•Hr = Wr•Hb时,k•Wr的蓝色糖果和k•Wb的红色糖果的花费与价值是相同的,所以我们不妨假设蓝色糖果数量X是 < Wr的。

综上所述,我们在求解该题时,分以下几种不同的情况分别选取决策方案:


  1. 某种糖x花费Wx ≥ C ,枚举x的数目
  2. 两种糖花费都 < C

  1. Wb•Hr ≥ Wr•Hb,蓝色糖数目 < Wr,枚举蓝色糖果数目
  2. Wb•Hr < Wr•Hb,红色糖数目 < Wb,枚举红色糖果数目

My Code

//  Created by Chlerry in 2015.
//  Copyright (c) 2015 Chlerry. All rights reserved.
//  http://codeforces.com/contest/526/problem/C

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define ll long long

ll C,Hr,Hb,Wr,Wb;

ll enumerate(ll lim,ll W1,ll H1)
{
    ll maxn=0,W2=Wr+Wb-W1,H2=Hr+Hb-H1;
    for(ll i=0;i<=lim;i++)
        if(i*W1<=C && i*H1+(C-i*W1)/W2*H2>maxn)
            maxn=i*H1+(C-i*W1)/W2*H2;
    return maxn;
}

int main()
{
    //freopen("in.txt","r",stdin);
    scanf("%lld %lld %lld %lld %lld",&C,&Hr,&Hb,&Wr,&Wb);
    ll SQRT=(int)sqrt((double)C);
    if(Wb>=SQRT)
        cout<<enumerate(SQRT,Wb,Hb);
    else if(Wr>=SQRT)
        cout<<enumerate(SQRT,Wr,Hr);
    else if(Wb*Hr>=Wr*Hb)
        cout<<enumerate(Wr,Wb,Hb);
    else //if(Wb*Hr<Wr*Hb)
        cout<<enumerate(Wb,Wr,Hr);
    cout<<endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值