• hdu 3016 dp+线段树


    Man Down

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

    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
    
    
    /*
    hdu 3016 dp+线段树
    感觉一直没什么思路
    由于每次我们只能从两边跳下来,倒推的答案是唯一的
    于是先按照高度排个序,然后就类似于叠木板,先判断当前的两端下面是哪个木板
    然后取较大值再加上当前值即可
    study~~~
    hhh-2016-02-29 22:54:23
    */
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <iostream>
    #include <cstring>
    #include <map>
    using namespace std;
    typedef long long ll;
    const int maxn = 100050;
    const int inf = 0x3f3f3f3f;
    struct node
    {
        int l,r,mid;
        int cov;
    } tree[maxn<<2];
    
    struct segment
    {
        int l,r,h;
        int val;
        void get()
        {
            scanf("%d%d%d%d",&h,&l,&r,&val);
        }
    } seg[maxn];
    int dp[maxn];
    bool cmp(segment a,segment b)
    {
        return a.h < b.h;
    }
    
    void push_up(int r)
    {
    }
    
    void build(int i,int l,int r)
    {
        tree[i].l = l;
        tree[i].r = r;
        tree[i].cov = 0;
        tree[i].mid = (l +r)>>1;
        if(l == r)
        {
            return ;
        }
        build(i<<1,l,tree[i].mid);
        build(i<<1|1,tree[i].mid+1,r);
        push_up(i);
    }
    
    void push_down(int r)
    {
        int lson = r<<1,rson = r<<1|1;
        if(tree[r].cov != -1)
        {
            tree[lson].cov = tree[r].cov;
            tree[rson].cov = tree[r].cov;
            tree[r].cov = -1;
        }
    }
    
    void update(int i,int l,int r,int c)
    {
        if(tree[i].l >= l && tree[i].r <= r)
        {
            tree[i].cov = c;
            return ;
        }
        push_down(i);
        if(l <= tree[i].mid)
            update(i<<1,l,r,c);
        if(r > tree[i].mid)
            update(i<<1|1,l,r,c);
        push_up(i);
    }
    
    int query(int i,int k)
    {
        if(tree[i].cov != -1)
        {
            return tree[i].cov;
        }
        push_down(i);
        if(k <= tree[i].mid)
            return query(i<<1,k);
        else
            return query(i<<1|1,k);
        push_up(i);
    }
    
    int main()
    {
        int n;
        while(scanf("%d",&n) != EOF)
        {
            build(1,1,maxn);
            for(int i =1; i <= n; i++)
            {
                seg[i].get();
            }
            memset(dp,0,sizeof(dp));
            sort(seg+1,seg+n+1,cmp);
            int l=0,r=0;
            for(int i = 1; i <= n; i++)
            {
                if(i > 1)
                {
                    l = query(1,seg[i].l);
                    r = query(1,seg[i].r);
                }
                dp[i] = max(dp[l],dp[r])+seg[i].val;
                update(1,seg[i].l,seg[i].r,i);
            }
            dp[n] += 100;
            if(dp[n] <= 0)
                printf("-1
    ");
            else
                printf("%d
    ",dp[n]);
        }
        return 0;
    }
    

      





  • 相关阅读:
    Java中class的前面添加public和不添加public有什么区别?
    java中构造函数和一般函数的区别
    Java里的new
    【Python】正则表达式
    【Java】仿真qq尝试:用户注册(二)
    【Java】流与文件(端口 & 文件读写对象)
    【Java】仿真qq尝试:用户注册(一)
    【Java】仿真qq尝试:聊天界面 && 响应用户输入
    【JavaScript】canvas实现一个小游戏
    【JavaScript】键盘控制小球
  • 原文地址:https://www.cnblogs.com/Przz/p/5409613.html
Copyright © 2020-2023  润新知