hdu 4091 Zombie’s Treasure Chest 贪心+枚举

转自:http://blog.csdn.net/a601025382s/article/details/12308193

题意:

输入背包体积n,绿宝石体积s1,价值v1,蓝宝石体积s2,价值v2,宝石数目无限,问背包里能放下的最大价值?

题解:

看过去很像完全背包,可数据很大(虽然没给出,也能猜到,不然太水了),所以不能用背包求。又只有两种物品,想到了贪心,将价值与体积比大(称为价值比)的优先放入。但体积限制,这样还不可以,还需要枚举减少价值比大的宝石个数,是否可以增大所求价值。又我们可以知道对于体积是m=lcm(s1,s2)背包,肯定全选价值比大的。所以至多只要枚举n-n/m+m的体积。如果小于这个值,存在大于m的空余,这个空余肯定用价值大的放置。

注意:

1.不够一个公倍数的时候,计算需要小心。。我就出错了。。

2.枚举的时候,跨度选择max(s1,s2),这个算是优化吧,没有的话会TLE

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cstdio>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<map>
  9 #include<set>
 10 #include<string>
 11 //#include<pair>
 12
 13 #define N 10000005
 14 #define M 1005
 15 #define mod 1000000007
 16 //#define p 10000007
 17 #define inf 0x3f3f3f3f
 18 #define mod2 100000000
 19 #define ll long long
 20 #define LL long long
 21 #define maxi(a,b) (a)>(b)? (a) : (b)
 22 #define mini(a,b) (a)<(b)? (a) : (b)
 23
 24 using namespace std;
 25
 26 ll n,s1,v1,s2,v2;
 27 ll ans;
 28 ll ma;
 29 ll g;
 30 ll c1,c2;
 31 ll ans1,ans2;
 32
 33 ll gcd(ll a,ll b)
 34 {
 35     return b==0?a:gcd(b,a%b);
 36 }
 37 ll lcm(ll a,ll b)
 38 {
 39     return a/gcd(a,b)*b;
 40 }
 41
 42 void ini()
 43 {
 44     ans=0;
 45     ans1=ans2=0;
 46     scanf("%I64d%I64d%I64d%I64d%I64d",&n,&s1,&v1,&s2,&v2);
 47     g=lcm(s1,s2);
 48     c1=g/s1;
 49     c2=g/s2;
 50 }
 51
 52
 53
 54 void solve()
 55 {
 56     if(n/g>=1)
 57         ans1=(n/g-1)*max(c1*v1,c2*v2);
 58     ll i;
 59     if(s1<s2){
 60         swap(c1,c2);
 61         swap(s1,s2);
 62         swap(v1,v2);
 63     }
 64     ll left=n%g;
 65     if(n/g>=1)
 66         left+=g;
 67     ll en=left/s1;
 68     ll re;
 69     for(i=0;i<=en;i++){
 70         re=i*v1+(left-i*s1)/s2*v2;
 71         ans2=max(ans2,re);
 72     }
 73     ans=ans1+ans2;
 74 }
 75
 76 void out()
 77 {
 78     printf("%I64d\n",ans);
 79 }
 80
 81 int main()
 82 {
 83     int T;
 84     //freopen("data.in","r",stdin);
 85     //freopen("data.out","w",stdout);
 86     scanf("%d",&T);
 87     for(int ccnt=1;ccnt<=T;ccnt++)
 88    // while(T--)
 89    // while(scanf(""))
 90     {
 91         //if(n==0 && k==0 ) break;
 92
 93         ini();
 94         solve();
 95         printf("Case #%d: ",ccnt);
 96         out();
 97     }
 98
 99     return 0;
100 }
时间: 2024-08-04 14:10:04

hdu 4091 Zombie’s Treasure Chest 贪心+枚举的相关文章

UVa 12325 - Zombie&#39;s Treasure Chest-[分类枚举]

12325 Zombie’s Treasure Chest Some brave warriors come to a lost village. They are very lucky and find a lot of treasures and a big treasure chest, but with angry zombies. The warriors are so brave that they decide to defeat the zombies and then brin

一道看似dp实则暴力的题 Zombie&#39;s Treasure Chest

 Zombie's Treasure Chest 本题题意:有一个给定容量的大箱子,此箱子只能装蓝宝石和绿宝石,假设蓝绿宝石的数量无限,给定蓝绿宝石的大小和价值,要求是获得最大的价值 题解:本题看似是dp中的背包问题,但是由于数据量太大,用dp肯定会超时,所以只能寻找另外一种思路,可以用贪心加暴力,先求出两种宝石大小的最小公倍数com,然后将N/com-com,与N%comkanchengs看成是两个部分(想想应该明白).将前一个部分,放入单位价值量最高的那个,对于后面那个部分直接将S1的数量从

12325 - Zombie&#39;s Treasure Chest.

简单枚举+巧妙躲避大枚举量 #include<bits/stdc++.h> using namespace std; long long n,s1,v1,s2,v2,total; int main() { ios::sync_with_stdio(false); long long T,maxn=0; cin>>T; while(T--) { cin>>n>>s1>>v1>>s2>>v2; total=0; long lo

【例题 7-11 UVA - 12325】Zombie&#39;s Treasure Chest

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 1.N/s1 < 1e6 枚举1的个数 2.N/s2<1e6 枚举2的个数 3.s1和s2的值较小 假设买了s2个1和s1个2 那么这两种物品占的体积就一样大了. 即都为s1s2 而第一种物品价值为s2v1第二种物品价值为s1v2 那么 如果s2v1>s1v2的话. 可以想见,如果第二种物品的数量超过了s1的话,显然可以把它占的体积都用来买物品1,因为那样更优. 则我们第二种物品最多只要枚举到s1就可以了. 同理s2v1

UVa 12325 Zombie&#39;s Treasure Chest【暴力】

题意:和上次的cf的ZeptoLab的C一样,是紫书的例题7-11 不过在uva上交的时候,用%I64d交的话是wa,直接cout就好了 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map> 8 #include&l

hdu4091 Zombie’s Treasure Chest

Zombie’s Treasure Chest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4442    Accepted Submission(s): 889 Problem Description Some brave warriors come to a lost village. They are very lucky an

UVa12325, Zombie&#39;s Treasure Chest

反正书上讲的把我搞得晕头转向的,本来就困,越敲越晕...... 转网上一个大神写的吧,他分析的很好(个人感觉比书上的清楚多了) 转:http://blog.csdn.net/u010536683/article/details/12450865 UVa12325, Zombie's Treasure Chest

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

hdu4901Zombie’s Treasure Chest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5006    Accepted Submission(s): 1032 Problem Description Some brave warriors come to a lost village. They are very lucky and find a lot of treasu