HDU 3016 Man Down (线段树+dp)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1391 Accepted Submission(s): 483
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).
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
代码
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int maxn=100000; struct node{ int l,r,id; int lazy; }a[4*maxn]; struct line{ int l,r,lid,rid,h,c,id; bool friend operator < (line x,line y){ return x.h<y.h; } }data[maxn+100]; int n,dp[maxn]; void input(){ data[0].l=0,data[0].r=100000,data[0].h=0,data[0].c=0; for(int i=1;i<=n;i++){ scanf("%d%d%d%d",&data[i].h,&data[i].l,&data[i].r,&data[i].c); } sort(data,data+n+1); for(int i=0;i<=n;i++){ dp[i]=-1;data[i].id=i; data[i].lid=-1;data[i].rid=-1; } } void build(int l,int r,int k){ a[k].l=l;a[k].r=r;a[k].id=0;a[k].lazy=-1; if(l<r){ int mid=(l+r)/2; build(l,mid,2*k); build(mid+1,r,2*k+1); } } void pushDown(int k){ if(a[k].lazy<0) return; a[2*k].lazy=a[k].lazy; a[2*k].id=a[k].lazy; a[2*k+1].lazy=a[k].lazy; a[2*k+1].id=a[k].lazy; a[k].lazy=-1; } int query(int l,int r,int k){ if(l<=a[k].l && a[k].r<=r){ return a[k].id; }else{ pushDown(k); int mid=(a[k].l+a[k].r)/2; if(r<=mid) return query(l,r,2*k); else return query(l,r,2*k+1); } } void insert(int l,int r,int k,int id){ if(l<=a[k].l && a[k].r<=r){ a[k].id=id; a[k].lazy=id; }else{ pushDown(k); int mid=(a[k].l+a[k].r)/2; if(r<=mid) insert(l,r,2*k,id); else if(l>=mid+1) insert(l,r,2*k+1,id); else{ insert(l,mid,2*k,id); insert(mid+1,r,2*k+1,id); } } } void computing(){ build(0,maxn,1); for(int i=1;i<=n;i++){ data[i].lid=query(data[i].l,data[i].l,1); data[i].rid=query(data[i].r,data[i].r,1); insert(data[i].l,data[i].r,1,data[i].id); } dp[n]=100+data[n].c; for(int i=n;i>=1;i--){ //cout<<data[i].h<<" "<<data[i].lid<<" "<<data[i].rid<<endl; if(data[i].lid>=0){ int id=data[i].lid; dp[id]=max(dp[id],dp[i]+data[id].c); } if(data[i].rid>=0){ int id=data[i].rid; dp[id]=max(dp[id],dp[i]+data[id].c); } } if(dp[0]>0) cout<<dp[0]<<endl; else cout<<"-1"<<endl; } int main(){ while(scanf("%d",&n)!=EOF){ input(); computing(); } return 0; }