Man Down
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2869 Accepted Submission(s): 1043
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
Recommend
gaojie | We have carefully selected several similar problems for you: 1542 1828 1540 2871 1166
#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<queue>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll unsigned long long
#define MAX 1000000000
#define ms memset
#define lrt int l,int r,int rt
#define lson l , mid , rt<<1
#define rson (mid+1) , r, (rt<<1|1)
#define INF -100000000
using namespace std;
const int maxn=1e5+5;
const int N=1e5+5;
/*
题目大意:给定高度不同的区间快和其权重。
要求最后能收获的最大值。
用dp的思想解决,
对于一个区间来说,其经过这个区间的最优值肯定是
从这个区间的最大值出发,即以前高度累计的最大值。
所以我们主要维护一个区间的最大值即可
(刚开始还很傻的维护了区间和,后来发现不需要的)
然后按高度从高到低往下扫描,
如果区间的最大值大于零则两端更新,当然区间间的任意值更新为无穷小。
*/
struct node
{
int h,l,r,v;
node(){}
};
node planks[maxn];
bool cmp(node x,node y){return x.h>y.h;}
int lazy[maxn<<2],maxv[maxn<<2];///线段树的构建
///区间查询,单点更新
void pushup(int rt)
{
maxv[rt]=max(maxv[rt<<1],maxv[rt<<1|1]);
}
void pushdown(int rt,int l,int r)
{
if(l==r) return;
int mid=l+r>>1;
if(lazy[rt])
{
lazy[rt<<1]=lazy[rt];
lazy[rt<<1|1]=lazy[rt];
maxv[rt<<1]=lazy[rt];
maxv[rt<<1|1]=lazy[rt];
lazy[rt]=0;
}
}
void Build(lrt)
{
lazy[rt]=0;///
if(l==r) {maxv[rt]=0;return ;}
int mid=l+r>>1;
Build(l,mid,rt<<1);
Build(mid+1,r,rt<<1|1);
pushup(rt);///
}
void update(lrt,int L,int R,int v)///单点更新
{
if(L<=l&&r<=R)
{
maxv[rt]=v;
lazy[rt]=v;
return ;
}
pushdown(rt,l,r);
int mid=l+r>>1;
if(L<=mid) update(lson,L,R,v);
if(mid<R) update(rson,L,R,v);
pushup(rt);
}
int query(lrt,int L,int R)
{
if(L<=l&&r<=R) return maxv[rt];
pushdown(rt,l,r);
int mid=l+r>>1,ans=0;
if(L<=mid) ans=max(ans,query(lson,L,R));
if(mid<R) ans=max(ans,query(rson,L,R));
return ans;
}
int n;
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++) scanf("%d%d%d%d",&planks[i].h,&planks[i].l,&planks[i].r,&planks[i].v);
sort(planks,planks+n,cmp);
planks[0].v+=100;
Build(1,N,1);
update(1,N,1,planks[0].l,planks[0].l,planks[0].v);
update(1,N,1,planks[0].r,planks[0].r,planks[0].v);
for(int i=1;i<n;i++)
{
/// if(maxv[1]<=0) break;
int q=query(1,N,1,planks[i].l,planks[i].r);
update(1,N,1,planks[i].l,planks[i].r,INF);///全部更新为最小值,区间上的点肯定不能用,能扩展的状态在两端
if(q>0)///||(q<=0&&!i))
{
/// cout<<query(1,N,1,planks[i].l,planks[i].r)<<endl;
update(1,N,1,planks[i].l,planks[i].l,planks[i].v+q);
update(1,N,1,planks[i].r,planks[i].r,planks[i].v+q);
///cout<<query(1,N,1,planks[i].l,planks[i].r)<<endl;
}
}
printf("%d\n",maxv[1]<=0?-1:maxv[1]);
}
return 0;
}
/*
3
10 2 10 20
5 1 5 -120
4 6 11 -120
*/