Man Down(线段树 + 区间覆盖 + DP)

Man Down

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


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

 

       题意:

       游戏规则跟“是男人就下100层”一样。给出 N (2 ~ 100000)块板,每块板都有一个高度,左端值,右端值,价值。满血的时候是100,从最高那块板开始往下垂直跳落,问到达底部时候的最大值,如果总和值小于等于 0 ,则说明不能达到,则输出 -1。

 

        思路:

        线段树 + DP + 区间覆盖 + 单点查询。DP 的时候要从高往低(正向),根据板的左右端点值所在的区域来判断能到达哪一层,如果没有任何一层可以到达则说明直接到达底部,即 0 层。首先线段树预处理每块板能到达的下一层分别是什么。所以要从最低的一层开始往上覆盖(逆向),先查询左右端点分别落在哪里,再将这块板覆盖到线段树中。然后根据公式:

        dp[ p[i].rl ] = max(dp[ p[i].rl ], dp[i] + p[ p[i].rl ].val);

        dp[ p[i].rr ] = max(dp[ p[i].rr ], dp[i] + p[ p[i].rr ].val); 来更新值,rl,rr,代表这个板的左右端点能分别到达哪块板,dp[ i ] 代表到达这块板时候的最大值。

        最后判断如果 dp [ 0 ] <= 0,则输出 -1,否则输出最大值 dp [ 0 ]。这道题的 DP 方式很像倒三角求最大和那题的 DP 方式。

        注意查询的时候,当一找到有板覆盖就返回值,否则一直找,当找到 l == r 的时候依然没有板覆盖则返回 0,说明这块板直接到达底部。

 

        AC:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX = 100005;

typedef struct {
        int h, l, r, val;
        int rl, rr;
} node;

node p[MAX];
int dp[MAX];

int cover[MAX * 3];

bool cmp(node a, node b) {
        return a.h < b.h;
}

void push_down(int node, int l, int r) {
        if (cover[node] != -1) {
                cover[node << 1] = cover[node];
                cover[node << 1 | 1] = cover[node];
                cover[node] = -1;
        }
}


void build (int node, int l, int r) {
        if (l == r) {
                cover[node] = -1;
        } else {
                int mid = (r + l) >> 1;
                build(node << 1, l, mid);
                build(node << 1 | 1, mid + 1, r);
                cover[node] = -1;
        }
}

void updata (int node, int l, int r, int cl, int cr, int c) {
        if (cl > r || cr < l) return;
        if (cl <= l && cr >= r) {
                cover[node] = c;
                return;
        }

        push_down(node, l, r);
        int mid = (r + l) >> 1;
        if (cl <= mid) updata(node << 1, l, mid, cl, cr, c);
        if (cr > mid) updata(node << 1 | 1, mid + 1, r, cl, cr, c);
}

int query (int node, int l, int r, int c) {
        if (cover[node] != -1) return cover[node];
        if (l == r && cover[node] == -1) return 0;

        push_down(node, l, r);
        int mid = (r + l) >> 1;
        if (c <= mid) return query(node << 1, l, mid, c);
        return query(node << 1 | 1, mid + 1, r, c);
}

int main() {
        int n;

        while (~scanf("%d", &n)) {
                int Max_r = 0;

                for (int i = 1; i <= n; ++i) {
                        scanf("%d%d%d%d", &p[i].h, &p[i].l, &p[i].r, &p[i].val);
                        Max_r = max(Max_r, p[i].r);
                }

                sort(p + 1, p + n + 1, cmp);

                build(1, 1, Max_r);

                for (int i = 1; i <= n; ++i) {
                        p[i].rl = query(1, 1, Max_r, p[i].l);
                        p[i].rr = query(1, 1, Max_r, p[i].r);
                        updata(1, 1, Max_r, p[i].l, p[i].r, i);
                }

                memset(dp, -1, sizeof(dp));
                dp[n] = 100 + p[n].val;
                for (int i = n; i >= 1; --i) {
                        dp[ p[i].rl ] = max(dp[ p[i].rl ], dp[i] + p[ p[i].rl ].val);
                        dp[ p[i].rr ] = max(dp[ p[i].rr ], dp[i] + p[ p[i].rr ].val);
                }

                if (dp[0] <= 0) printf("-1\n");
                else printf("%d\n", dp[0]);
        }

        return 0;
}

 

        

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值