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
2009 Multi-University Training Contest 12 - Host by FZU
注意多组数据!
虽然说这题大家应该都知道是用线段树,但是为什么呢?
当然,看到这题首先想的不应该是线段树,而是dp。dp还是非常明显,一般人都想得出来。每块板子可以落到哪几块板子上就连一条边,形成一个DAG,在DAG上进行dp就行了。
所以为什么要用线段树呢,在这题中可以发现如果直接dp,时间复杂度会达到n^2,所以用线段树查询每块板子能落到哪几块板子上,将一个n优化成logn。
但是各个板子高低不等啊,所以先把所有板子从低到高进行排序,然后从低到高遍历一遍,先查询左右两边由哪块最高的板覆盖,再将这块板加入线段树中。
Code:
#include<iostream>标签:HDU3016,right,int,value,Down,he,left,dp,Man From: https://blog.51cto.com/u_15888102/5878472
#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;
}