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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
运动员最佳匹配问题是指在给定的n个男运动员和n个女运动员中,找到最佳的n对组合,使得这n对组合的分数和最大。每个男运动员都有一个评分表,记录他对所有女运动员的评分,同样,每个女运动员也有一个评分表,记录她对所有男运动员的评分。匹配的分数是男女之间的互相评分之和。 这个问题可以通过排列树进行求解。排列树是一种搜索树,其中每个节点表示了一个待定的选项,而每个分支代表一个选项的选择。在这个问题中,树的深度为n,每个节点表示了一对男女的匹配情况,分支代表了下一对待匹配的男女。对于每个节点,可以通过计算当前已匹配的男女对的分数和,加上剩余男女之间的最大分数和来估计当前的分数上限。如果当前的分数上限已经小于已知的最大分数和,则可以剪枝掉这个节点,因为它不可能包含最优解。 以下是伪代码实现: ``` best_score = 0 current_score = 0 matched_pairs = [] def permute(men, women): global best_score, current_score, matched_pairs # 如果已经匹配了n对,更新最优解 if len(matched_pairs) == len(men): best_score = max(best_score, current_score) return # 计算剩余男女之间的最大分数和 max_possible_score = 0 for man in men: if man not in matched_pairs: max_possible_score += max(man.ratings.values()) for woman in women: if woman not in matched_pairs: max_possible_score += max(woman.ratings.values()) # 如果当前分数加上剩余分数的最大值小于已知最优解,剪枝 if current_score + max_possible_score < best_score: return # 选择下一对男女进行匹配 for man in men: if man not in matched_pairs: for woman in women: if woman not in matched_pairs: matched_pairs.append((man, woman)) current_score += man.ratings[woman] current_score += woman.ratings[man] permute(men, women) current_score -= man.ratings[woman] current_score -= woman.ratings[man] matched_pairs.remove((man, woman)) ``` 在这个伪代码中,`men`和`women`分别是男女运动员的列表,每个运动员都有一个`ratings`属性,记录他们对其他所有运动员的评分。`best_score`记录了已知的最大分数和,`current_score`记录了当前已匹配的男女对的分数和,`matched_pairs`记录了当前已匹配的男女对。`permute`函数是排列树的递归函数,它会枚举下一对待匹配的男女,计算当前分数上限,判断是否需要剪枝,然后递归地进行搜索。在递归过程中,会更新当前分数和已匹配的男女对,并且在回溯时撤销这些修改。 这个算法的时间复杂度是O(n!),因为它需要枚举n!种可能的匹配组合。但是,由于有剪枝优化,实际运行时间通常会比这个时间复杂度低得多。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值