题意:cow 想要在太空搭电梯,每一种block有它的高度hi和最高不能超过多高ai 每种block有一定的数量ci 求能搭建的高大高度
思路:多重背包。 只是有一点小变形 把block按它的的ai从小到大排序,从小的开始搭建 因为如果从大的开始,就有可能把小的给忽略掉 用dp[] 记录状态 能否达到这个高度 然后在每次比较时 选出当前能达到的最大高度
//332K
172MS
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define Max 40010
#define M 450
using namespace std;
int ans;
int dp[Max];
struct data
{
int h,a,c;
}node[M];
int max (int a, int b)
{
return a > b ? a : b;
}
bool cmp (data a,data b)
{
return a.a < b.a;
}
void MulPack(int cost,int amount,int cap)
{
int i;
if (cost*amount >= cap)
{
for (i = cost;i <= cap;i ++)
if (dp[i - cost])
{
dp[i] = 1;
ans = max (ans,i);
//每次选择当前能达到的最大高度,下面的也一样
}
return ;
}
int k = 1;
while (k < amount)
{
for (i = cap;i >= k*cost;i --)
if (dp[i - k*cost])
{
dp[i] = 1;
ans = max (ans,i);
}
amount -= k;
k *= 2;
}
for (i = cap;i >= amount*cost;i --)
if (dp[i - amount*cost])
{
dp[i] = 1;
ans = max(ans,i);
}
}
int main ()
{
int n,i;
while (~scanf ("%d",&n))
{
memset (dp,0,sizeof (dp));
dp[0] = 1;
//高度为0 一定能达到
for (i = 0;i < n;i ++)
scanf ("%d%d%d",&node[i].h,&node[i].a,&node[i].c);
sort (node,node+n,cmp);
//以node[].a 排序
ans = 0;
for (i = 0;i < n;i ++)
MulPack(node[i].h,node[i].c,node[i].a);
printf ("%d\n",ans);
}
}
思路:多重背包。 只是有一点小变形 把block按它的的ai从小到大排序,从小的开始搭建 因为如果从大的开始,就有可能把小的给忽略掉 用dp[] 记录状态 能否达到这个高度 然后在每次比较时 选出当前能达到的最大高度
//332K
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define Max 40010
#define M 450
using namespace std;
int ans;
int dp[Max];
struct data
{
}node[M];
int max (int a, int b)
{
}
bool cmp (data a,data b)
{
}
void MulPack(int cost,int amount,int cap)
{
}
int main ()
{
}