Man Down
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 50 Accepted Submission(s) : 25
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.
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
是男人就下100层。这个男人开始在最顶层,
并且拥有100的能量,如果他能下到他下面的横木上,
他将无条件获得横木的val。
他只能从横木的左端点和右端点下落。
要是中途他的能量小于或等于0,he will die
*/
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#define N 100012
using namespace std;
int dp[N][2];
int n;
struct node
{
int l,r;
int h;
int val;
}blank[N];
struct node2
{
int l,r;
int h;
int logal;
}tree[N*4];
int cmp(node a,node b)
{
return a.h>b.h;//倒着排,第一块肯定是最高的。
}
void build(int id,int l,int r)
{
tree[id].l=l;
tree[id].r=r;
tree[id].logal=-1;
if(l==r)
return;
int mid=(l+r)/2;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
}
void update(int id,int l,int r,int val)
{
if(l<=tree[id].l&&r>=tree[id].r)
{
tree[id].logal=val;
return;
}
if(tree[id].logal!=-1)
{
tree[id*2].logal=tree[id*2+1].logal=tree[id].logal;
tree[id].logal=-1;
}
int mid=(tree[id].l+tree[id].r)/2;
if(r<=mid)
{
update(id*2,l,r,val);
}
else if(l>mid)
update(id*2+1,l,r,val);
else
{
update(id*2,l,mid,val);
update(id*2+1,mid+1,r,val);
}
}
int query(int p,int pos)
{
if(tree[p].l==tree[p].r)
return tree[p].logal;
if(tree[p].logal!=-1)
{
tree[p*2].logal=tree[p*2+1].logal=tree[p].logal;
tree[p].logal=-1;
}
int mid=(tree[p].l+tree[p].r)/2;
if(pos<=mid)
return query(p*2,pos);
else
return query(p*2+1,pos);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(dp,-1,sizeof(dp));
int left=100000;
int right=-100000;
for(int i=0;i<n;i++)
{
scanf("%d%d%d%d",&blank[i].h,&blank[i].l,&blank[i].r,&blank[i].val);
left=min(left,blank[i].l);
right=max(right,blank[i].r);
}
build(1,left,right);
sort(blank,blank+n,cmp);
/**/
for(int i=n-1;i>=0;--i)
{
int j=query(1,blank[i].l);//表示第几块木板
if(j!=-1)
{
dp[i][0]=max(dp[j][0],dp[j][1])+blank[j].val;//第j块木板的价值
}
else
{
dp[i][0]=0;
}
j=query(1,blank[i].r);
if(j!=-1)
{
dp[i][1]=max(dp[j][0],dp[j][1])+blank[j].val;
}
else
dp[i][1]=0;
update(1,blank[i].l,blank[i].r,i);
}
int ans=max(dp[0][1],dp[0][0])+100+blank[0].val;
if(ans<=0)
ans=-1;
cout<<ans<<endl;
}
return 0;
}
本文介绍了一个简化版的“ManDown100 Floors”游戏算法问题,玩家需要控制角色通过不同高度的木板,获取能量并最终成功到达地面。文章详细解释了游戏规则,并提供了一段C++代码实现,利用线段树等数据结构来解决该问题。

被折叠的 条评论
为什么被折叠?



