题意:给n个三元组,每个由三个数组成,第一个是每个积木高度,第二个是当前层以下积木高度允许的最大值,第三个是积木数目,求搭建的最大高度。
思路:让最小的允许高度处在最低处,依次向上搭建。
use数组表示这种积木已经用了多少块,dp[j]表示:当高度为j时,有没有符合条件的搭建情况,dp[j-a[i].h]其实表示上一块积木有没有被用到,那么最终答案就是,最大的j了。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define maxn 40004
typedef long long ll;
#define inf 0x3f3f3f3f
struct node
{
int h,l,c;
}a[maxn];
int dp[maxn];
bool cmp(node a,node b)
{
if(a.l==b.l) return a.c<b.c;
else return a.l<b.l;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d%d",&a[i].h,&a[i].l,&a[i].c);
}
sort(a+1,a+n+1,cmp);
memset(dp,0,sizeof(dp));
dp[0]=1;
int ans=0,use[maxn];
for(int i=1;i<=n;i++){
memset(use,0,sizeof(use));
for(int j=a[i].h;j<=a[i].l;j++){
if(!dp[j]&&dp[j-a[i].h]&&use[j-a[i].h]<a[i].c){
use[j]=use[j-a[i].h]+1;
//cout<<use[j-a[i].h]<<" "<<(j-a[i].h)/a[i].h<<endl;
dp[j]=1;
if(ans<j) ans=j;
}
}
}
cout<<ans<<endl;
}