题目连接:https://cn.vjudge.net/contest/202335#problem/L
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.
* Line 1: A single integer H, the maximum height of a tower that can be built
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.
题意:通过用每种型号的模块构建最高的塔; 每种模块高度不同,数量有限,并且在空间中存在的高度有限。
多重背包转换为完全背包!!!
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int h,a,c;
};
int f[40010],v[40010];//f数组中存放几种类型模块可搭建的高度,v数组存放块数
int cmp(node s,node b)
{
return s.a<b.a;
}
int main()
{
int n;
scanf("%d",&n);
node num[n];
for(int i=0;i<n;i++)
scanf("%d %d %d",&num[i].h,&num[i].a,&num[i].c);
sort(num,num+n,cmp);
memset(f,0,sizeof(f));
f[0]=1;
int ans=0;
for(int i=0;i<n;i++)
{
memset(v,0,sizeof(v));
for(int j=num[i].h;j<=num[i].a;j++)
{
if(!f[j]&&f[j-num[i].h]&&v[j-num[i].h]<num[i].c)
{
f[j]=1;
v[j]=v[j-num[i].h]+1;
if(ans<j)
ans=j;
//printf("j-num[%d].h=%d num[%d].h=%d\n",i,j-num[i].h,i,num[i].h);
//printf("ans=%d\n",ans);
}
}
}
printf("%d\n",ans);
return 0;
}
/*数组f中通过j++循环存放可能拼成的所有高度
如果该种拼法高度存在,则f[i]=1,否则f[i]=0;
通过if判断增加新拼法,进而更新高度
v数组存放在每种限制(a)下的块数
ans记录更新最高高度*/