解题报告:POJ 3162 Walking Race 树型DP+单调RMQ

Walking Race
Time Limit: 10000MS Memory Limit: 131072K
Total Submissions: 3838 Accepted: 956
Case Time Limit: 3000MS

Description

flymouse’s sister wc is very capable at sports and her favorite event is walking race. Chasing after the championship in an important competition, she comes to a training center to attend a training course. The center has N check-points numbered 1 through N. Some pairs of check-points are directly connected by two-way paths. The check-points and the paths form exactly a tree-like structure. The course lasts N days. On the i-th day, wc picks check-point i as the starting point and chooses another check-point as the finishing point and walks along the only simple path between the two points for the day’s training. Her choice of finishing point will make it that the resulting path will be the longest among those of all possible choices.

After every day’s training, flymouse will do a physical examination from which data will obtained and analyzed to help wc’s future training be better instructed. In order to make the results reliable, flymouse is not using data all from N days for analysis. flymouse’s model for analysis requires data from a series of consecutive days during which the difference between the longest and the shortest distances wc walks cannot exceed a bound M. The longer the series is, the more accurate the results are. flymouse wants to know the number of days in such a longest series. Can you do the job for him?

Input

The input contains a single test case. The test case starts with a line containing the integers N (N ≤ 106) and M (M < 109). Then follow N − 1 lines, each containing two integers fi and di (i = 1, 2, …, N − 1), meaning the check-points i + 1 and fi are connected by a path of length di.

Output

Output one line with only the desired number of days in the longest series.

Sample Input

3 2
1 1
1 3

Sample Output

3

Hint

Explanation for the sample:

There are three check-points. Two paths of lengths 1 and 3 connect check-points 2 and 3 to check-point 1. The three paths along with wc walks are 1-3, 2-1-3 and 3-1-2. And their lengths are 3, 4 and 4. Therefore data from all three days can be used for analysis.

Source

[Submit]   [Go Back]   [Status]   [Discuss]


题意:

给定一棵树,有个人第i天从i节点跑到能跑的最远节点,有n个节点,然后将n天跑步的距离记录下,求一个连续区间满足区间最大值最小值之差小于等于m,输出区间最大长度

思路:

这个题分成两个部分

第一部分:

求树上所有点的最远点距,同HDU_2196,这部分的详解见 点击打开链接

第二部分:

给定一个序列,求最长的区间满足   区间最大最小值之差小于等于m

首先肯定会有尺取,重点在于如何移动标记的同时,快速更新最大最小值

这个可以当做RMQ问题来做,每次都重新查询一遍,用线段树,树状数组,ST可以在Nlog(N)内解决,满足题目的要求,比较基础,就不细讲

记录O(n)的单调队列的做法


每个数有两个属性,大和小,最大值看大属性,最小值看小属性

移动右标记时
    比 新加入的数 大的 所有左边的数 的 小 属性都没有价值了
    比 新加入的数 小的 所有左边的数 的 大 属性都没有价值了
移动左标记时
    直接弹出较左的队头

因此维护两个大属性和小属性的单调双向队列就行了


代码:

#include<vector>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>


using namespace std;
struct edge{
    int t,w;
    edge(){}
    edge(int _t,int _w){
        t = _t;
        w = _w;
    }
};



int n,m;
long long dp[2][1000005];
vector<edge>G[1000005];
void dfs1(int x){
    dp[0][x]=0LL;
    for(int i=0;i<G[x].size();i++){
        edge &j = G[x][i];
        dfs1(j.t);
        dp[0][x] = max(dp[0][x],dp[0][j.t]+j.w);
    }
}

void dfs2(int x,int fa=0){
    dp[1][x]=dp[1][fa];
    int w = 0;
    for(int i=0;i<G[fa].size();i++){
        edge &j = G[fa][i];
        if(j.t!=x) dp[1][x] = max(dp[1][x],dp[0][j.t]+j.w);
        else w = j.w;
    }dp[1][x] += w;
    for(int i=0;i<G[x].size();i++)
        dfs2(G[x][i].t,x);
}

int smx,emx,smi,emi;
int Qmx[1000005];
int Qmi[1000005];

int main()
{
    while(scanf("%d%d",&n,&m)==2){
        for(int i=1;i<=n;i++)G[i].clear();
        for(int i=2,f,w;i<=n;i++){
            scanf("%d%d",&f,&w);
            G[f].push_back(edge(i,w));
        }dfs1(1);dfs2(1);
        for(int i=1;i<=n;i++)dp[0][i] = max(dp[0][i],dp[1][i]);
        long long *d = dp[0];
        int ans = 0;
        Qmx[1]=Qmi[1]=1;
        smx=emx=smi=emi=1;
        int s = 1, e = 1;
        while(e<=n){
            while(d[Qmx[smx]]-d[Qmi[smi]]<=m){
                ans = max(ans,e-s+1);
                if(++e>n)break;
                while(emx>=smx&&d[Qmx[emx]]<=d[e])emx--;Qmx[++emx]=e;
                while(emi>=smi&&d[Qmi[emi]]>=d[e])emi--;Qmi[++emi]=e;
            }
            bool mx = Qmx[smx]<=Qmi[smi] , mi = Qmx[smx]>=Qmi[smi];
            if(mx) s = Qmx[smx++]+1;
            if(mi) s = Qmi[smi++]+1;
        }printf("%d\n",ans);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值