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>
 9 #include<deque>
10 #include<stack>
11 #include<bitset>
12 #include<algorithm>
13 #include<functional>
14 #include<numeric>
15 #include<utility>
16 #include<sstream>
17 #include<iostream>
18 #include<iomanip>
19 #include<cstdio>
20 #include<cmath>
21 #include<cstdlib>
22 #include<cstring>
23 #include<ctime>
24 #define LL long long
25 
26 using namespace std;
27 LL c,h1,h2,w1,w2;
28 LL count(LL v)
29 {
30   LL ans = 0 ; 
31   ans = h1*v;
32   ans += (LL)((LL)(c-w1*v)/w2) * h2;
33   return ans;
34 }
35 LL ans = 0 ; 
36 LL gcd(LL a, LL b)
37 {
38     return b==0 ?a:gcd(b,a%b);
39 }
40 int main(){
41     scanf("%I64d %I64d %I64d %I64d %I64d",&c,&h1,&h2,&w1,&w2);
42     LL lcm = w1/gcd(w1,w2)*w2;
43     LL tmp = (c/lcm) - 1; 
44     if(tmp >= 1)
45     {
46          ans = tmp  * max((lcm/w1)*h1,(lcm/w2)*h2) ;
47         c = c % lcm + lcm ;
48     }
49     if(w1 < w2)
50     {
51        swap(h1,h2);
52        swap(w1,w2);
53     }
54     LL mx = 0 ;
55     //printf("%I64d %I64d %I64d\n",ans,lcm,c);
56     for(LL i = 0;i * w1 <= c ;i ++)
57     {
58         mx = max(mx,count(i)) ;     
59     }
60     printf("%I64d\n",ans + mx);
61 return 0;
62 }
View Code

 

转载于:https://www.cnblogs.com/zyue/p/4394783.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值