hdu 3016 Man Down(简单线段树&简单DP)

Man Down

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


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

Recommend
gaojie

题意:

题目是以"是男人就下一百层"游戏为背景。不过加以简化了。就说只有两种木板了。一种到达上面后可以得到上面的分数。另一种就减相应的分数。每一个木板会给你它的左右端点的坐标。[xl,xr]。然后小人会到达高度比当前木板低。

且满足。xl'<=xl<=xr'或。xl'<=xr<=xr'。的最近木板上。因为它只能从当前木板的左边或右边下去嘛。

如果不存在这样的木板。他就完成了它的使命。(因为玩过这个游戏的缘故我开始以为他挂掉了然后wa了数次)

还有如果中途分数小于或等于0的话那么它就挂掉了。初始分为100.

思路:

由于这是在线段树专题里找的题。于是拼命往线段树上想。白白YY那么久。其实这题就是一简单DP因为对于一个木板要么从左边下去。要么从右边下去。但由于高度不连续所以处理有点不同。这题关键在处理最近上。我们可以用一颗线段树来维护。先把木板高度按高度升序排序。然后对于当前木板。查一下它左右端点被谁覆盖了用数组记录下来。因为是升序排序的。查到的一定是最近的。然后再用这木板去覆盖点。循环做。我开始想着从终点倒着走回去

就不用记录了。但发现有bug。因为有可能存在正这走挂了。倒着走不挂的情况。

4
5 2 6 100
4 1 4 -200
3 5 8 -200
2 1 10 500

所以只能走。

详细见代码:

#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100010;
int dp[maxn],id[maxn<<2],li[maxn],ri[maxn];
struct node
{
    int h,xl,xr,val;
} plan[maxn];
bool cmp(node a,node b)
{
    return a.h<b.h;
}
void btree(int L,int R,int k)
{
    int ls,rs,mid;
    id[k]=0;
    if(L==R)
        return ;
    ls=k<<1;
    rs=ls|1;
    mid=(L+R)>>1;
    btree(L,mid,ls);
    btree(mid+1,R,rs);
}
void pushdown(int k)
{
    int ls,rs;
    ls=k<<1;
    rs=ls|1;
    id[ls]=id[rs]=id[k];
    id[k]=0;
}
void update(int L,int R,int l,int r,int k,int d)
{
    int ls,rs,mid;
    if(l==L&&r==R)
    {
        id[k]=d;
        return;
    }
    if(id[k])
        pushdown(k);
    ls=k<<1;
    rs=ls|1;
    mid=(L+R)>>1;
    if(l>mid)
        update(mid+1,R,l,r,rs,d);
    else if(r<=mid)
        update(L,mid,l,r,ls,d);
    else
    {
        update(L,mid,l,mid,ls,d);
        update(mid+1,R,mid+1,r,rs,d);
    }
}
int qu(int L,int R,int p,int k)
{
    int ls,rs,mid;
    if(L==R||id[k])
        return id[k];
    ls=k<<1;
    rs=ls|1;
    mid=(L+R)>>1;
    if(p>mid)
        return qu(mid+1,R,p,rs);
    else
        return qu(L,mid,p,ls);
}
int main()
{
    int n,i,lim;

    while(~scanf("%d",&n))
    {
        lim=0;
        plan[0].val=0;
        dp[0]=-INF;
        for(i=1;i<=n;i++)
        {
            scanf("%d%d%d%d",&plan[i].h,&plan[i].xl,&plan[i].xr,&plan[i].val);
            lim=max(lim,plan[i].xr);
            dp[i]=-INF;
        }
        sort(plan+1,plan+n+1,cmp);
        btree(1,lim,1);
        for(i=1;i<=n;i++)
        {
            li[i]=qu(1,lim,plan[i].xl,1);
            ri[i]=qu(1,lim,plan[i].xr,1);
            update(1,lim,plan[i].xl,plan[i].xr,1,i);
        }
        dp[n]=100+plan[n].val;
        for(i=n;i>=1;i--)
        {
            if(dp[i]>0)//题目数据有点水。不加这个判断也能过。
            {
                dp[li[i]]=max(dp[li[i]],dp[i]+plan[li[i]].val);
                dp[ri[i]]=max(dp[ri[i]],dp[i]+plan[ri[i]].val);
            }
        }
        if(dp[0]<=0)
            dp[0]=-1;
        printf("%d\n",dp[0]);
    }
    return 0;
}
/*
4
5 2 6 100
4 1 4 -200
3 5 8 -200
2 1 10 500
2
5 2 6 100
4 1 4 -200
*/
/*
-1//数据水了。不是-1也行。
100
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: hdu 2829 Lawrence 斜率优化dp 这道题是一道经典的斜率优化dp题目,需要用到单调队列的思想。 题目大意是给定一个序列a,求出一个序列b,使得b[i]表示a[1]~a[i]中的最小值,且满足b[i] = min{b[j] + (i-j)*k},其中k为给定的常数。 我们可以将上式拆开,得到b[i] = min{b[j] - j*k} + i*k,即b[i] = i*k + min{b[j] - j*k},这个式子就是斜率优化dp的形式。 我们可以用单调队列来维护min{b[j] - j*k},具体思路如下: 1. 首先将第一个元素加入队列中。 2. 从第二个元素开始,我们需要将当前元素加入队列中,并且需要维护队列的单调性。 3. 维护单调性的方法是,我们从队列的末尾开始,将队列中所有大于当前元素的元素弹出,直到队列为空或者队列中最后一个元素小于当前元素为止。 4. 弹出元素的同时,我们需要计算它们对应的斜率,即(b[j]-j*k)/(j-i),并将这些斜率与当前元素的斜率比较,如果当前元素的斜率更小,则将当前元素加入队列中。 5. 最后队列中的第一个元素就是min{b[j] - j*k},我们将它加上i*k就得到了b[i]的值。 6. 重复以上步骤直到处理完所有元素。 具体实现可以参考下面的代码: ### 回答2: HDU 2829 Lawrence 斜率优化 DP 是一道经典的斜率优化 DP 题目,其思想是通过维护一个下凸包来优化 DP 算法。下面我们来具体分析一下这道题目。 首先,让我们看一下该题目的描述。题目给定一些木棒,要求我们将这些木棒割成一些给定长度,且要求每种长度的木棒的数量都是一样的,求最小的割枝次数。这是一个典型的背包问题,而且在此基础上还要求每种长度的木棒的数量相同,这就需要我们在状态设计上走一些弯路。 我们来看一下状态的定义。定义 $dp[i][j]$ 表示前 $i$ 个木棒中正好能割出 $j$ 根长度为 $c_i$ 的木棒的最小割枝次数。对于每个 $dp[i][j]$,我们可以分类讨论: 1. 不选当前的木棒,即 $dp[i][j]=dp[i-1][j]$; 2. 选当前的木棒,即 $dp[i][j-k]=dp[i-1][j-k]+k$,其中 $k$ 是 $j/c_i$ 的整数部分。 现在问题再次转化为我们需要在满足等量限制的情况下,求最小的割枝次数。可以看出,这是一个依赖于 $c_i$ 的限制。于是,我们可以通过斜率优化 DP 来解决这个问题。 我们来具体分析一下斜率优化 DP 算法的思路。我们首先来看一下动态规划的状态转移方程 $dp[i][j]=\min\{dp[i-1][k]+x_k(i,j)\}$。可以发现,$dp[i][j]$ 的最小值只与 $dp[i-1][k]$ 和 $x_k(i,j)$ 有关。其中,$x_k(i,j)$ 表示斜率,其值为 $dp[i-1][k]-k\times c_i+j\times c_i$。 接下来,我们需要维护一个下凸包,并通过斜率进行优化。我们具体分析一下该过程。假设我们当前要计算 $dp[i][j]$。首先,我们需要找到当前点 $(i,j)$ 在凸包上的位置,即斜率最小值的位置。然后,我们根据该位置的斜率计算 $dp[i][j]$ 的值。接下来,我们需要将当前点 $(i,j)$ 加入到下凸包上。 我们在加入点的时候需要注意几点。首先,我们需要将凸包中所有斜率比当前点小的点移除,直到该点能够加入到凸包中为止。其次,我们需要判断该点是否能够加入到凸包中。如果不能加入到凸包中,则直接舍弃。最后,我们需要保证凸包中斜率是单调递增的,这就需要在加入新的点之后进行上一步操作。 以上就是该题目的解题思路。需要注意的是,斜率优化 DP 算法并不是万能的,其使用情况需要根据具体的问题情况来确定。同时,该算法中需要维护一个下凸包,可能会增加一些算法的复杂度,建议和常规 DP 算法进行对比,选择最优的算法进行解题。 ### 回答3: 斜率优化DP是一种动态规划优化算法,其主要思路是通过对状态转移方程进行变形,提高算法的时间复杂度。HDU2829 Lawrence问题可以用斜率优化DP解决。 首先,我们需要了解原问题的含义。问题描述如下:有$n$个人在数轴上,第$i$个人的位置为$A_i$,每个人可以携带一定大小的行李,第$i$个人的行李重量为$B_i$,但是每个人只能帮助没有他们重量大的人搬行李。若第$i$个人搬运了第$j$个人的行李,那么第$i$个人会累加$C_{i,j}=\left|A_i-A_j\right|\cdot B_j$的体力消耗。求$m$个人帮助每个人搬运行李的最小体力消耗。 我们可以通过斜率优化DP解决这个问题。记$f_i$为到前$i$个人的最小体力消耗,那么状态转移方程为: $$f_i=\min_{j&lt;i}\{f_j+abs(A_i-A_j)\cdot B_i\}$$ 如果直接使用该方程,时间复杂度为$O(n^2)$,如果$n=10^4$,则需要计算$10^8$次,运算时间极长。斜率优化DP通过一些数学推导将方程变形,将时间复杂度降低到$O(n)$,大大缩短了计算时间。 通过斜率优化DP的推导式子,我们可以得到转移方程为: $$f_i=\min_{j&lt;i}\{f_j+slope(j,i)\}$$ 其中,$slope(j,i)$表示直线$j-i$的斜率。我们可以通过如下方式来求解$slope(j,i)$: $$slope(j,i)=\frac{f_i-f_j}{A_i-A_j}-B_i-B_j$$ 如果$slope(j,i)\leq slope(j,k)$,那么$j$一定不是最优,可以直接舍去,降低计算时间。该算法的时间复杂度为$O(n)$。 综上所述,斜率优化DP是一种动态规划优化算法,可以大大缩短计算时间。在处理类似HDU2829 Lawrence问题的时候,斜率优化DP可以很好地解决问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值