hdu 3016 Man Down

Posted Kaiser

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 3016 Man Down相关的知识,希望对你有一定的参考价值。

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).

InputThere 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.OutputIf 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

题解:先按高度排序,然后倒着来,每次覆盖一段区间,然后简单dp,为左右两个端点查询的最大值。然后判断会不会
为负,即可。
技术分享图片
  1 #include<cstdio>
  2 #include<algorithm>
  3 using namespace std;
  4 const int maxn= 200001;
  5 const int mincost=-200000000;
  6 struct Pool
  7 {
  8     int to,pre;
  9 }pool[maxn<<1];
 10 int p[maxn<<1],top,n;
 11 int dp[maxn];
 12 void addedge(int x,int y)
 13 {
 14     pool[++top].to=y;
 15     pool[top].pre=p[x];
 16     p[x]=top;
 17 }
 18 struct datb
 19 {
 20     int h,xl,xr,value;
 21     bool operator <(const datb &t)const
 22     {
 23         return h<t.h;
 24     }
 25 }d[maxn];
 26 int m[maxn<<2],lazy[maxn<<2];
 27 bool defi[maxn<<2];
 28 void pushdown(int rt)
 29 {
 30     if(lazy[rt]!=-1)
 31     {
 32         m[rt]=lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
 33         defi[rt]=true,lazy[rt]=-1;
 34     }
 35 }
 36 void pushup(int rt)
 37 {
 38     if(defi[rt<<1]==true&&defi[rt<<1|1]==true&&m[rt<<1]==m[rt<<1|1])
 39     {
 40         defi[rt]=true;
 41         m[rt]=m[rt<<1];
 42     }
 43     else
 44     {
 45         defi[rt]=false;
 46     }
 47 }
 48 void init()
 49 {
 50     m[1]=0;
 51     defi[1]=true;
 52     lazy[1]=0;
 53     top=0;
 54     for(int i=0;i<=n;i++)
 55     {
 56         p[i]=0;
 57         dp[i]=mincost;
 58     }
 59 }
 60 void change(int rt,int l,int r,int x,int y,int s)
 61 {
 62     if(x<=l&&y>=r)
 63     {
 64         m[rt]=s;
 65         lazy[rt]=s;
 66         defi[rt]=true;
 67         return ;
 68     }
 69     pushdown(rt);
 70     int mid=(l+r)>>1;
 71     if(mid>=x)
 72     change(rt<<1,l,mid,x,y,s);
 73     if(mid<y)
 74     change(rt<<1|1,mid+1,r,x,y,s);
 75     pushup(rt);
 76 }
 77 int find(int rt,int l,int r,int k)
 78 {
 79     pushdown(rt);
 80     if(r>=k&&l<=k&&defi[rt]==true)
 81     return m[rt];
 82     int mid=(l+r)>>1;
 83     if(mid>=k)
 84     return find(rt<<1,l,mid,k);
 85     return find(rt<<1|1,mid+1,r,k);
 86 }
 87 
 88 int main()
 89 {
 90     int t1,t2;
 91     while(scanf("%d",&n)!=EOF)
 92     {
 93         init();
 94         for(int i=1;i<=n;i++)
 95         scanf("%d%d%d%d",&d[i].h,&d[i].xl,&d[i].xr,&d[i].value);
 96         sort(d+1,d+1+n);
 97         top=0;
 98         for(int i=1;i<=n;i++)
 99         {
100             t1=find(1,1,100000,d[i].xl);
101             t2=find(1,1,100000,d[i].xr);
102             addedge(i,t1);
103             addedge(i,t2);
104             change(1,1,100000,d[i].xl,d[i].xr,i);
105         }
106         dp[n]=d[n].value+100;
107         for(int i=n;i>=1;i--)
108         {
109             for(int j=p[i];j;j=pool[j].pre)
110             {
111                 int t=pool[j].to;
112                 dp[t]=max(dp[t],dp[i]+d[t].value);
113             }
114         }
115         if(dp[0]<=0)
116         printf("-1\n");
117         else
118         printf("%d\n",dp[0]);
119     }
120 }
View Code

 

 

以上是关于hdu 3016 Man Down的主要内容,如果未能解决你的问题,请参考以下文章

HDU3016 Man Down 题解

Man Down(线段树dp)

HDU 3440 House Man

hdu3440House Man差分约束系统

[HDU2577]How to Type(DP)

hdu3440 House Man 差分约束系统