您的位置:首页 > 其它

POJ 2392 Space Elevator

2016-03-22 20:30 190 查看
排序+背包。

先对按高度限制从小到大排序,然后做背包即可。0/1背包300多ms过的,可以用完全背包二进制优化。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxn=400+10;
int n;
struct X
{
int h,c,a;
}s[maxn];
int dp[400000+10];
int ans;

bool cmp(const X&a,const X&b)
{
return a.a<b.a;
}
void read()
{
for(int i=1;i<=n;i++)
scanf("%d%d%d",&s[i].h,&s[i].a,&s[i].c);
sort(s+1,s+1+n,cmp);
}

void init()
{
memset(dp,0,sizeof dp); dp[0]=1;
ans=0;
}

void work()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=s[i].c;j++)
{
for(int p=s[i].a;p>=s[i].h;p--)
{
if(dp[p-s[i].h]==1)
{
dp[p]=1;
ans=max(ans,p);
}
}
}
}
printf("%d\n",ans);
}

int main()
{
while(~scanf("%d",&n))
{
read();
init();
work();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: