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
",ans,lcm,c);
56     for(LL i = 0;i * w1 <= c ;i ++)
57     {
58         mx = max(mx,count(i)) ;     
59     }
60     printf("%I64d
",ans + mx);
61 return 0;
62 }
View Code
原文地址:https://www.cnblogs.com/zyue/p/4394783.html