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) K ( 1 <= K <= 400 ) different types of blocks with which to build the tower. Each block of type i has height hi(1<=hi<=100) h i ( 1 <= h i <= 100 ) and is available in quantity ci(1<=ci<=10) 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 ai(1<=ai<=40000) 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
K
Lines : Each line contains three space-separated integers:
hi,ai
h
i
,
a
i
, and
ci
c
i
. Line
i+1
i
+
1
describes block type
i
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
Hint
OUTPUT DETAILS:
From the bottom: 3 blocks of type 2, below 3 of type 1, below 6 of type 3. Stacking 4 blocks of type 2 and 3 of type 1 is not legal, since the top of the last type 1 block would exceed height 40.
题意:
给种箱子,第
i
i
种箱子有个,箱子的高度是
hi
h
i
,第
i
i
种箱子不能放在超过高度的地方。请问用这n种箱子最多可以搭多高的塔。
题解:
可行性背包DP+贪心。我们可以知道,如果我们确定了要用每种箱子用多少个,那么我们会尽量让
ai
a
i
比较小的箱子放在下面。那么我们就先把箱子按照
ai
a
i
排序。然后做一个可行性DP:
f[0]=1
for i = 1 -> n
for j=1 -> c[i]
for k = a[i] downto h[i]
f[k] |= f[k-h[i]]
然后 f[i]==1 f [ i ] == 1 就说明可以搭到 i i <script type="math/tex" id="MathJax-Element-3398">i</script>的高度
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define LiangJiaJun main
#define ll long long
using namespace std;
int n;
struct Ds{
int a,h,c;
}d[404];
int f[40004];
inline bool dex(Ds A,Ds B){
return A.a<B.a;
}
int w33ha(){
memset(f,0,sizeof(f));
int sum=0;
for(int i=1;i<=n;i++){
scanf("%d%d%d",&d[i].h,&d[i].a,&d[i].c);
sum+=d[i].h*d[i].c;
}
sort(d+1,d+n+1,dex);
f[0]=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=d[i].c;j++){
for(int k=min(sum,d[i].a);k>=d[i].h;k--){
if(f[k-d[i].h]){
f[k]=1;
}
}
}
}
for(int i=40000;i>=0;i--){
if(f[i])return printf("%d\n",i),0;
}
return 0;
}
int LiangJiaJun (){
while(scanf("%d",&n)!=EOF)w33ha();
return 0;
}