A - Space Elevator 多重背包(稍有变动)

Description

The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 <= K <= 400) different types of blocks with which to build the tower. Each block of type i has height h_i (1 <= h_i <= 100) and is available in quantity c_i (1 <= c_i <= 10). Due to possible damage caused by cosmic rays, no part of a block of type i can exceed a maximum altitude a_i (1 <= a_i <= 40000). 

Help the cows build the tallest space elevator possible by stacking blocks on top of each other according to the rules.

Input

* Line 1: A single integer, K 

* Lines 2..K+1: Each line contains three space-separated integers: h_i, a_i, and c_i. Line i+1 describes block type i.

Output

* Line 1: A single integer H, the maximum height of a tower that can be built

Sample Input

3
7 40 3
5 23 8
2 52 6

Sample Output

48
***********************************************************************************************************************************************************多重背包
***********************************************************************************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<algorithm>
 6 using namespace std;
 7 int dp[500010],i,j,k;
 8 int n;
 9 struct ds
10 {
11     int a,h,c;
12 }e[500001];
13 bool cmp(ds x,ds y)
14 {
15     return x.c<y.c;
16 }
17 void zeropack(int cost,int w)
18  {
19      for(int it=w;it>=cost;it--)
20       if(dp[it]<dp[it-cost]+cost)
21         dp[it]=dp[it-cost]+cost;
22 
23  }
24  void completepack(int cost,int w)
25     {
26         for(int it=cost;it<=w;it++)
27          if(dp[it]<dp[it-cost]+cost)
28            dp[it]=dp[it-cost]+cost;
29     }
30 void multi(int cost,int am,int w)
31 {
32     int k;
33     if(cost*am>=w)
34      {
35          completepack(cost,w);
36          return;
37      }
38     k=1;
39     while(k<am)
40     {
41      zeropack(k*cost,w);
42      am-=k;
43      k=k<<1;
44     }
45     zeropack(cost*am,w);
46 
47 }
48 int main()
49 {
50     cin>>n;
51     int maxn=-1;
52     int sm=-1;
53     memset(dp,0,sizeof(dp));
54     for(i=0;i<n;i++)
55     {
56         cin>>e[i].h>>e[i].c>>e[i].a;
57         if(maxn<e[i].c)
58          maxn=e[i].c;
59     }
60     sort(e,e+n,cmp);
61     for(int i=0;i<n;i++)
62     {
63         multi(e[i].h,e[i].a,e[i].c);
64     }
65     for(i=0;i<=maxn;i++)
66      {
67          //cout<<dp[i]<<endl;
68          if(sm<dp[i])
69           sm=dp[i];
70      }
71     cout<<sm<<endl;
72     return 0;
73 
74 }
View Code
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<algorithm>
 6 using namespace std;
 7 int dp[500010],i,j,k;
 8 int n;
 9 struct ds
10 {
11     int a,h,c;
12 }e[500001];
13 bool cmp(ds x,ds y)
14 {
15     return x.c<y.c;
16 }
17 void zeropack(int cost,int w)
18  {
19      for(int it=w;it>=cost;it--)
20       if(dp[it]<dp[it-cost]+cost)
21         dp[it]=dp[it-cost]+cost;
22 
23  }
24  void completepack(int cost,int w)
25     {
26         for(int it=cost;it<=w;it++)
27          if(dp[it]<dp[it-cost]+cost)
28            dp[it]=dp[it-cost]+cost;
29     }
30 void multi(int cost,int am,int w)
31 {
32     int k;
33     if(cost*am>=w)
34      {
35          completepack(cost,w);
36          return;
37      }
38     k=1;
39     while(k<am)
40     {
41      zeropack(k*cost,w);
42      am-=k;
43      k=k<<1;
44     }
45     zeropack(cost*am,w);
46 
47 }
48 int main()
49 {
50     cin>>n;
51     int maxn=-1;
52     int sm=-1;
53     memset(dp,0,sizeof(dp));
54     for(i=0;i<n;i++)
55     {
56         cin>>e[i].h>>e[i].c>>e[i].a;
57         if(maxn<e[i].c)
58          maxn=e[i].c;
59     }
60     sort(e,e+n,cmp);
61     for(int i=0;i<n;i++)
62     {
63         multi(e[i].h,e[i].a,e[i].c);
64     }
65     for(i=0;i<=maxn;i++)
66      {
67          //cout<<dp[i]<<endl;
68          if(sm<dp[i])
69           sm=dp[i];
70      }
71     cout<<sm<<endl;
72     return 0;
73 
74 }
View Code

转载于:https://www.cnblogs.com/sdau--codeants/p/3353560.html

猜你喜欢

转载自blog.csdn.net/weixin_33928137/article/details/93432951