当前位置:网站首页>POJ2392 SpaceElevator [DP]

POJ2392 SpaceElevator [DP]

2022-07-07 23:23:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm the king of the whole stack .

The main idea of the topic : There is a cow going into space , He has many kinds of stones , The height of each stone is hi, But you can't put it ai Height above . And such stones have ci individual Stack these stones . Ask the highest height you can reach . Their thinking : First, sort the data in ascending order . This is a standard multiple backpack problem Why order ? Because only in this way can we get the optimal solution , Suppose the high one is in front at the beginning , Then there are low ones in the back, but you can't choose , Just choose the higher one . In this way, the optimal solution cannot be reached send f[i] The status flag of . Can it reach this height In this way, we can get f[i] in i The maximum value of .

Pay attention here max When assigning the initial value, it should be assigned as 0. Not for -1. Because the answer may be 0

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
int type;
int f[44444],usr[44444];
struct Block
{
	int h,a,c;
}block[555];
bool cmp(Block a,Block b)
{
	return a.a<b.a;
}
int main()
{
	scanf("%d",&type);
	for(int i=1;i<=type;i++)
	{
		scanf("%d%d%d",&block[i].h,&block[i].a,&block[i].c);
	}
	sort(block+1,block+1+type,cmp);
	int maxn=0;
	f[0]=1;
	for(int t=1;t<=type;t++)
	{
		memset(usr,0,sizeof(usr));
		for(int h=block[t].h;h<=block[t].a;h++)
		{
			if(!f[h] && f[h-block[t].h] && usr[h-block[t].h]+1<=block[t].c)
			{
				usr[h]=usr[h-block[t].h]+1;
				f[h]=1;
				maxn=max(h,maxn);
			}
		}
	}
	printf("%d\n",maxn);
	return 0;
}

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116214.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207072149335242.html