hdu 3016 Man Down
Man Down
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1146 Accepted Submission(s): 393
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
![](http://acm.hdu.edu.cn/data/images/exe3016-1.JPG)
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).
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.
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
Recommend
gaojie
//这题关键是思维转化、线段从下往上一段段覆盖,覆盖之前查看本线段的左端点和右端点是那条线段
//覆盖的就是线段编号、然后从上往下Dp
#include <iostream> #include <stdio.h> #include <string.h> #include <cmath> #include <algorithm> #include <queue> #define N 100003 #define lson l,m,k<<1 #define rson m+1,r,k<<1|1 using namespace std; int st[N<<2]; struct node { int lx,rx,val; int h; bool operator <(const node&a)const { return h<a.h; } }; node line[N]; int lid[N],rid[N],dp[N]; void build(int l,int r,int k) { st[k]=0; if(l==r) return; int m=(l+r)>>1; build(lson); build(rson); } void down(int &k) { st[k<<1]=st[k<<1|1]=st[k]; st[k]=-1; } int id; void update(int &L,int &R,int l,int r,int k) { if(L<=l&&R>=r) { st[k]=id; return; } if(st[k]!=-1) down(k); int m=(l+r)>>1; if(L<=m) update(L,R,lson); if(R>m) update(L,R,rson); } int query(int index,int l,int r,int k) { if(st[k]!=-1) return st[k]; int m=(l+r)>>1; if(index<=m) return query(index,lson); return query(index,rson); } int main() { int n; int i,k; while(scanf("%d",&n)!=EOF) { k=0;dp[0]=0; for(i=1;i<=n;i++) { dp[i]=0; scanf("%d%d%d%d",&line[i].h,&line[i].lx,&line[i].rx,&line[i].val); if(line[i].rx>k) k=line[i].rx; } sort(line+1,line+n+1); build(1,k,1); for(i=1;i<=n;i++) { lid[i]=query(line[i].lx,1,k,1); rid[i]=query(line[i].rx,1,k,1); id=i; update(line[i].lx,line[i].rx,1,k,1); } dp[n]=100+line[n].val; for(i=n;i>=1;i--) if(dp[i]>0) { dp[lid[i]]=max(dp[lid[i]],dp[i]+line[lid[i]].val); dp[rid[i]]=max(dp[rid[i]],dp[i]+line[rid[i]].val); } printf("%d\n",dp[0]>0?dp[0]:-1); } return 0; }