Codeforces 526C - Om Nom and Candies

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

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;
}
时间: 2024-07-29 18:37:46

Codeforces 526C - Om Nom and Candies的相关文章

Codeforces 526C - Om Nom and Candies(贪心,暴力)

题意:你最多可以吃C千克的糖,   有两种糖,每种糖有两个参数,一个为重 w  ,一个为欢乐度 h , 如何选择才能拥有最高的欢乐度,  两种糖数量不限. 题解:看了半天题解才理解如何做, 分为两种枚举政策涵盖了所有情况, 时间复杂度为sqrt(c),神奇的暴力 1.如果两种糖中重量最大的超过sqrt(c),  那么该糖最多也只能选择不超过sqrt(c)个,   直接枚举该糖个数,记录最大欢乐度 2.如果两种糖重量都小于sqrt(c),那么从性价比来考虑 我们可以买到Wb个a糖果的同时也可以将其

Codeforces C - Om Nom and Candies

C - Om Nom and Candies 思路:贪心+思维(或者叫数学).假设最大值max(wr,wb)为wr,当c/wr小于√c时,可以枚举r糖的数量(从0到c/wr),更新答案,复杂度√c:否则,假设hr/wr<hb/wr,得到hr*wb<hb*wr,由这个等式可知,在有wb*wr重量限制的情况下,买wb个r糖没有买wr个b糖划算,当需要买超过wb个r糖时,不如去买b糖,可以枚举r糖的数量(从0到wb-1),更新答案,复杂度√c. 代码: #include<bits/stdc++

Codeforces 525C Om Nom and Candies 枚举 + 复杂度分析

题意:给你无穷多的A,B物品,现在你有承重C的背包,给你A,B物品的价值和重量,问你如何取使得价值最大. 解题思路:很巧秒的枚举. 解题代码: 1 // File Name: c.cpp 2 // Author: darkdream 3 // Created Time: 2015年04月05日 星期日 01时16分14秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set>

ZeptoLab Code Rush 2015 C. Om Nom and Candies 暴力

C. Om Nom and Candies Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526/problem/C Description A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him

C. Om Nom and Candies 巧妙优化枚举,将复杂度控制在10e6

C. Om Nom and Candies 无线超大背包问题 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <

Codeforces 526D - Om Nom and Necklace 【KMP】

ZeptoLab Code Rush 2015 D. Om Nom and Necklace [题意] 给出一个字符串s,判断其各个前缀是否是 ABABA…ABA的形式(A和B都可以为空,且A有Q+1个,B有Q个,Q给定). [官方题解] 对于前缀P,我们可以把它拆成P=SSSS…SSSST,其中T是S的前缀.显然可以用KMP算法,时间复杂度是O(n). 当T=S:P=SSS…S.假设S出现了R次.如果转换为ABABAB…ABABA的形式,A和B是由几个S组成,而且最后的A一定是P的一个后缀.由

Codeforces 526D Om Nom and Necklace (KMP)

http://codeforces.com/problemset/problem/526/D 题意 给定一个串 T,对它的每一个前缀能否写成 A+B+A+B+...+B+A+B+A+B+...+B+A 的形式(k +1个 A,k 个 B,均可为空串) 分析 官方题解 对于前缀P,我们可以把它拆成P=SSSS…SSSST,其中T是S的前缀.显然可以用KMP算法,时间复杂度是O(n). 当T=S:P=SSS…S.假设S出现了R次.如果转换为ABABAB…ABABA的形式,A和B是由几个S组成,而且最

Codeforces 526D Om Nom and Necklace kmp+hash

题目链接:点击打开链接 题意: 给出长度为n的字符串,常数k 下面一个长度为n的字符串. 问: for(int i = 1; i <= n; i++){ 字符串的前i个字符 能否构成 形如A+B+A+B+A+B+A的形式,其中A有k+1个,B有k个 A和B是2个任意的字符串(也可以为空串) 若可以构成则输出1,否则输出0 } 思路: POJ1961 先用kmp求一个前缀循环节,. 我们观察 ABABABA => AB, AB, AB, A 所以前缀循环节有K个,而后面的A是尽可能地和AB长度接

Codeforces 526B Om Nom and Dark Park 树形dp

题意:给你一颗完全二叉树,每条边有一个值,可以对这个值进行加操作,让你满足根节点到所有叶子节点路径值相同  ,问你最少要加多少值. 解题思路:从上往下树形DP,位运算会比较方便. 解题代码: 1 // File Name: b.cpp 2 // Author: darkdream 3 // Created Time: 2015年04月05日 星期日 00时47分32秒 4 5 #include<vector> 6 #include<list> 7 #include<map&g