Man Down(线段树+区间更新+单点查询+dp)

来源:互联网 发布:淘宝商品分类管理 编辑:程序博客网 时间:2024/05/22 02:29

Man Down

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 26   Accepted Submission(s) : 13
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
410 5 10 105 3 6 -1004 7 11 202 2 1000 10
 

Sample Output
140
 

Source
2009 Multi-University Training Contest 12 - Host by FZU
 

Statistic | Submit | Back

题意:

就是一个man从 最高处的横木往下跳,在这里与我们平时的游戏有点出入,在此处 我们只能往最近的,并且包含我左或右端点的一个横木区间上跳。man的初始血量为100,每跳到一个横木上他的血量都有可能发生变化,当横木上放着食物的时候,man的血回increase,如果是障碍之类的就损耗血量。

思路:

求最大值,首先想到的是运用dp,显示按高度有小到大进行排序,然后运用线段树类似于染色问题,然后从低往高 进行插入,并且 查询包含其左或者右端点的 最近的 下面的区间:查找每块木板左边和右边的木板时只需要在线段树上找左边和右边坐标被谁覆盖就好。注意dp动态方程的查找

代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)#define maxn 100010int dp[maxn];struct node{int ll,rr;int h;int v;void get(){scanf("%d%d%d%d",&h,&ll,&rr,&v);}}p[maxn];struct Tree{int l;int r;int num;}tree[maxn<<2];bool cmp(node a,node b){return a.h<b.h;}void build(int l,int r,int i){tree[i].l=l;tree[i].r=r;tree[i].num=0;if(l==r)return;int mid=(l+r)>>1;build(l,mid,L(i));build(mid+1,r,R(i));}void update(int l,int r,int v,int i){if(tree[i].l==l&&tree[i].r==r){tree[i].num=v;//染色return;}if(tree[i].l==tree[i].r)return;if(tree[i].num){   //染色问题,看是否被覆盖tree[L(i)].num=tree[R(i)].num=tree[i].num;tree[i].num=0;}int mid=(tree[i].l+tree[i].r)>>1;if(mid>=r)update(l,r,v,L(i));else if(mid<l) update(l,r,v,R(i));else{update(l,mid,v,L(i));update(mid+1,r,v,R(i));}}int query(int l,int r,int i){if(tree[i].num){return tree[i].num;}if(tree[i].l==tree[i].r){return 0;}int mid=(tree[i].l+tree[i].r)>>1;if(r<=mid)return query(l,r,L(i));elsereturn query(l,r,R(i));}int main(){int n;while(scanf("%d",&n)!=EOF){int i,maxx=0;for(i=1;i<=n;i++){p[i].get();if(p[i].rr>maxx)maxx=p[i].rr;}build(1,maxx,1);sort(p+1,p+n+1,cmp);dp[0]=0;for(i=1;i<=n;i++){int l=query(p[i].ll,p[i].ll,1);int r=query(p[i].rr,p[i].rr,1);update(p[i].ll,p[i].rr,i,1);int ans=max(dp[l],dp[r]);dp[i]=ans+p[i].v;//状态转移方程}if(dp[n]+100>=0)printf("%d\n",dp[n]+100);elseprintf("-1\n");}    return 0;}




阅读全文
0 0
原创粉丝点击