HDU3016 Man Down

Man Down

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1562    Accepted Submission(s): 551



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

题意:空间有很多木板,每个木板有高度,长度(就是左边能达到的左边和右边能达到的),能量,一个人要从最高处下去,问到地面的最大能量石多少,只可以垂直下降,也就是说这个木板下面没有其他木板也可以直接到地面(但题目要求能量要求 最大)。
分析:要求出最大的能量先要把下去的路径找出来,所以先用线段树找出所有可能的路径,把输入的高度从小到大排序,然后把长度从小到大添进线段树,每加一条就要记录路径,因为这样刚好能反映从高处到低处的路径,而线段树里的附加信息记录的就是这条线段的相对高度,一直添加到最高,然后沿着找出的路径做个DP(从高处到低处,所以要从n开始)。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=100010;
struct seg
{
    int l,r;
    int mark;
}tree[MAXN*3];
struct Node
{
    int h,l,r,w;
}p[MAXN];
int dp[MAXN];
struct PTAH
{
    int l,r;
}path[MAXN];
bool cmp(Node x,Node y)
{
    return x.h<y.h;
}
void build(int l,int r,int k)
{
    tree[k].l=l;
    tree[k].r=r;
    tree[k].mark=0;
    if(l==r)
        return;
    int mid=(l+r)>>1;
    build(l,mid,k<<1);
    build(mid+1,r,k<<1|1);
}
void update(int l,int r,int k,int c)
{
    if(tree[k].l==l&&tree[k].r==r)
    {
        tree[k].mark=c;
        return;
    }
    if(tree[k].mark!=-1)
    {
        tree[k<<1].mark=tree[k<<1|1].mark=tree[k].mark;
        tree[k].mark=-1;
    }
    int mid=(tree[k].l+tree[k].r)>>1;
    if(r<=mid)
        update(l,r,k<<1,c);
    else if(l>mid)
        update(l,r,k<<1|1,c);
    else
    {
        update(l,mid,k<<1,c);
        update(mid+1,r,k<<1|1,c);
    }
}
int query(int n,int k)
{
    if(n<=tree[k].r&&tree[k].l<=n&&tree[k].mark!=-1)
        return tree[k].mark;
    int mid=(tree[k].l+tree[k].r)>>1;
    if(n<=mid)
        return query(n,k<<1);
    else
        return query(n,k<<1|1);
}
int main()
{
    int n,i;
    freopen("in.txt","r",stdin);
    while(scanf("%d",&n)==1)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d%d%d%d",&p[i].h,&p[i].l,&p[i].r,&p[i].w);
        }
        sort(p+1,p+n+1,cmp);
        build(1,100000,1);
        for(i=1;i<=n;i++)
        {
            path[i].l=query(p[i].l,1);
            path[i].r=query(p[i].r,1);
            update(p[i].l,p[i].r,1,i);
        }
        memset(dp,0,sizeof(dp));
        dp[n]=100+p[n].w;
        for(i=n;i>=1;i--)
        {
            dp[path[i].l]=max(dp[path[i].l],dp[i]+p[path[i].l].w);
            dp[path[i].r]=max(dp[path[i].r],dp[i]+p[path[i].r].w);
        }
        if(dp[0]>0)
            printf("%d\n",dp[0]);
        else
            printf("-1\n");
    }
    return 0;
}


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值