HDU3016-Man Down

Man Down

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2809    Accepted Submission(s): 1024
Problem Description
The Game “Man Down 100 floors” is an famous and interesting game.You can enjoy the game from
http://hi.baidu.com/abcdxyzk/blog/item/16398781b4f2a5d1bd3e1eed.html

We take a simplified version of this game. We have only two kinds of planks. One kind of the planks contains food and the other one contains nails. And if the man falls on the plank which contains food his energy will increase but if he falls on the plank which contains nails his energy will decrease. The man can only fall down vertically .We assume that the energy he can increase is unlimited and no borders exist on the left and the right.
First the man has total energy 100 and stands on the topmost plank of all. Then he can choose to go left or right to fall down. If he falls down from the position (Xi,Yi),he will fall onto the nearest plank which satisfies (xl <= xi <= xr)(xl is the leftmost position of the plank and xr is the rightmost).If no planks satisfies that, the man will fall onto the floor and he finishes his mission. But if the man’s energy is below or equal to 0 , he will die and the game is Over.
Now give you the height and position of all planks. And ask you whether the man can falls onto the floor successfully. If he can, try to calculate the maximum energy he can own when he is on the floor.(Assuming that the floor is infinite and its height is 0,and all the planks are located at different height).
Input
There are multiple test cases.
For each test case, The first line contains one integer N (2 <= N <= 100,000) representing the number of planks.
Then following N lines representing N planks, each line contain 4 integers (h,xl,xr,value)(h > 0, 0 < xl < xr < 100,000, -1000 <= value <= 1000), h represents the plank’s height, xl is the leftmost position of the plank and xr is the rightmost position. Value represents the energy the man will increase by( if value > 0) or decrease by( if value < 0) when he falls onto this plank.
Output
If the man can falls onto the floor successfully just output the maximum energy he can own when he is on the floor. But if the man can not fall down onto the floor anyway ,just output “-1”(not including the quote)
Sample Input
 
 
4
10 5 10 10
5 3 6 -100
4 7 11 20
2 2 1000 10
Sample Output
 
 
140
Source

注意多组数据!

虽然说这题大家应该都知道是用线段树,但是为什么呢?

当然,看到这题首先想的不应该是线段树,而是dp。dp还是非常明显,一般人都想得出来。每块板子可以落到哪几块板子上就连一条边,形成一个DAG,在DAG上进行dp就行了。

所以为什么要用线段树呢,在这题中可以发现如果直接dp,时间复杂度会达到n^2,所以用线段树查询每块板子能落到哪几块板子上,将一个n优化成logn。

但是各个板子高低不等啊,所以先把所有板子从低到高进行排序,然后从低到高遍历一遍,先查询左右两边由哪块最高的板覆盖,再将这块板加入线段树中。

Code:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#define N 100005
using namespace std;
int n,dp[N];
typedef long long ll;
struct node
{
	int high,left,right,value;
	int downleft,downright;
}a[N];
struct tree
{
	int left,right,mark,n;
}t[N*4];
int cmp(node x,node y)
{
	return x.high<y.high;
}
void build(int o,int l,int r)
{
	t[o].left=l;t[o].right=r;
	t[o].mark=0;t[o].n=-1;
	if(l==r)return;
	int mid=(l+r)>>1;
	build(o<<1,l,mid);
	build(o<<1|1,mid+1,r);
}
int query(int o,int x)
{
	if(t[o].left==t[o].right)return t[o].n;
	int mid=(t[o].left+t[o].right)>>1;
	if(t[o].mark!=-1)
	{
		t[o<<1].mark=t[o<<1|1].mark=t[o].mark;
		t[o<<1].n=t[o<<1|1].n=t[o].n;
		return t[o].n;
	}
	if(x<=mid)return query(o<<1,x);else
		return query(o<<1|1,x);
}
void updata(int o,int x,int l,int r)
{
	if(t[o].left==l&&t[o].right==r)
	{
		t[o].mark=1;
		t[o].n=x;
		return;
	}
	int mid=(t[o].left+t[o].right)>>1;
	if(t[o].mark!=-1)
	{
		t[o<<1].mark=t[o<<1|1].mark=t[o].mark;
		t[o<<1].n=t[o<<1|1].n=t[o].n;
		t[o].mark=-1;
	}
	if(r<=mid)updata(o<<1,x,l,r);else
		if(l>mid)updata(o<<1|1,x,l,r);else
		{
			updata(o<<1,x,l,mid);
			updata(o<<1|1,x,mid+1,r);
		}
}
int main()
{
	while(~scanf("%d",&n))
	{
		a[0].high=0;a[0].left=1;a[0].right=N;a[0].value=0;
		for(int i=1;i<=n;i++)
			scanf("%d%d%d%d",&a[i].high,&a[i].left,&a[i].right,&a[i].value);
		sort(a+1,a+1+n,cmp);
		build(1,1,N);
		for(int i=0;i<=n;i++)
		{
			a[i].downleft=query(1,a[i].left);
			a[i].downright=query(1,a[i].right);
			updata(1,i,a[i].left,a[i].right);
		}
		memset(dp,0,sizeof(dp));
		dp[n]=100+a[n].value;
		for(int i=n;i>=0;i--)
		{
			int l=a[i].downleft;
			int r=a[i].downright;
			dp[l]=max(dp[l],dp[i]+a[l].value);
			dp[r]=max(dp[r],dp[i]+a[r].value);
		}
		printf("%d\n",dp[0]=(dp[0]>0?dp[0]:-1));
	}
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JackflyDC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值