线段树+DP+hdu3016

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests

Man Down

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


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

思路:按照高度,从小到大排序,然后每次先查询当前线段做右端点可以下落到哪条线段,然后跟新本条线段。然后进行一个简单的dp,求的答案

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100100;
int n;
int dp[maxn];

struct node
{
    int xl,xr,val,id,h;
    int l,r;
    bool operator < (const node & a)const
    {
        return h<a.h;
    }
}p[maxn];


struct IntervalTree
{
    int id[maxn<<3];
    void build(int o,int l,int r)
    {
        id[o]=0;
        if(l==r)return;
        int mid=(l+r)>>1;
        build(o<<1,l,mid);
        build(o<<1|1,mid+1,r);
    }

    void pushdown(int o)
    {
        if(id[o]!=-1)
        {
            id[o<<1]=id[o<<1|1]=id[o];
            id[o]=-1;
        }
    }
    void update(int o,int l,int r,int q1,int q2,int pos)
    {

        if(l>r)return;
        if(q1<=l&&r<=q2)
        {
            id[o]=pos;
            return;
        }
        pushdown(o);
        int mid=(l+r)>>1;
        if(q1<=mid)update(o<<1,l,mid,q1,q2,pos);
        if(q2>mid)update(o<<1|1,mid+1,r,q1,q2,pos);
    }
    int query(int o,int l,int r,int pos)
    {
        if((id[o]!=-1)&&pos>=l&&pos<=r)return id[o];
        pushdown(o);
        int mid=(l+r)>>1;
        if(pos<=mid)return query(o<<1,l,mid,pos);
        else return query(o<<1|1,mid+1,r,pos);
    }
}tree;

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        int len=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d%d",&p[i].h,&p[i].xl,&p[i].xr,&p[i].val);
            p[i].id=i;
            len=max(len,p[i].xr);
        }
        sort(p+1,p+n+1);
        tree.build(1,1,len);
        for(int i=1;i<=n;i++)
        {
            p[i].l=tree.query(1,1,len,p[i].xl);
            p[i].r=tree.query(1,1,len,p[i].xr);
            tree.update(1,1,len,p[i].xl,p[i].xr,i);
        }
        memset(dp,0,sizeof(dp));
        dp[n]=100+p[n].val;
        for(int i=n;i>=1;i--)
        {
            int x=p[i].l,y=p[i].r;
            if(dp[i])
            {
                dp[x]=max(dp[x],dp[i]+p[p[i].l].val);
                dp[y]=max(dp[y],dp[i]+p[p[i].r].val);
            }
        }
        if(dp[0]>0)printf("%d\n",dp[0]);
        else printf("-1\n");
    }
    return 0;
}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值