POJ 3162 Walking Race (单调队列)@
来源:互联网 发布:淘宝多寄了东西怎么办 编辑:程序博客网 时间:2024/04/28 14:25
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 Ndays. 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?
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 fiare connected by a path of length di.
Output one line with only the desired number of days in the longest series.
3 21 11 3
3
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.
给定一颗树,求每个节点的最长路径,并求一个最长区间,这个区间的最大最小值差不超过m;
解:单调队列,类似于迟取法;
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#include <vector>#include <queue>using namespace std;typedef long long LL;const int N = 2e6+10;const int mod = 1e9+7;int head[N], cnt, dis[N], dis1[N], dis2[N];struct node{ int to, w, next;}p[N];void init(){ memset(head,-1,sizeof(head)); cnt=0;}void add(int x,int y,int z){ p[cnt].to=y, p[cnt].w=z, p[cnt].next=head[x],head[x]=cnt++; return ;}int pos, ans, n, m;int vis[N];struct Q{ int u, v;};void bfs(int x,int *s){ memset(vis,0,sizeof(vis)); s[x]=0, ans=0, vis[x]=1; queue<Q>q; Q tmp; tmp.u=x, tmp.v=0; q.push(tmp); while(!q.empty()) { tmp=q.front();q.pop(); int u=tmp.u, w=tmp.v; for(int i=head[u];i!=-1;i=p[i].next) { int v=p[i].to; if(vis[v]) continue; vis[v]=1; s[v]=w+p[i].w; if(s[v]>ans) ans=s[v], pos=v; tmp.u=v, tmp.v=s[v]; q.push(tmp); } } return ;}int xmin[N], xmax[N];int solve(){ int s1=0, s2=0, t1=0, t2=0,i, j=1; ans=0; for(i=1;i<=n;i++) { while(s1<t1&&dis[xmin[t1-1]]>=dis[i]) t1--; while(s2<t2&&dis[xmax[t2-1]]<=dis[i]) t2--; xmin[t1++]=i, xmax[t2++]=i; if(dis[xmax[s2]]-dis[xmin[s1]]>m) { ans=max(ans,i-j); while(dis[xmax[s2]]-dis[xmin[s1]]>m) { j=min(xmax[s2],xmin[s1])+1; while(s1<t1&&xmin[s1]<j) s1++; while(s2<t2&&xmax[s2]<j) s2++; } } } ans=max(ans,i-j); return ans;}int main(){ int x, w; while(scanf("%d %d", &n, &m)!=EOF) { init(); for(int i=1; i<n; i++) { scanf("%d %d", &x, &w); add(i+1,x, w); add(x,i+1, w); } bfs(1,dis); bfs(pos,dis1); bfs(pos,dis2); for(int i=1; i<=n; i++) { dis[i]=max(dis1[i], dis2[i]); } printf("%d\n",solve()); } return 0;}
- POJ 3162 Walking Race (单调队列)@
- POJ 3162(TreeDP+单调队列)Walking Race
- POJ 3162 Walking Race(树形DP + 单调队列)
- POJ 3162 Walking Race(树形DP+单调队列)
- POJ 3162 Walking Race TreeDP+双单调队列 ★
- POJ 3162Walking Race 树的直径加单调队列
- poj 3162 Walking Race 树形dp + 单调队列
- POJ 3162 Walking Race(单调队列或线段树加树型DP)
- POJ 3162 Walking Race 树的直径+单调队列(其实暴力也可以>_<)
- poj 3162 Walking Race (dfs+尺取法单调队列)★
- poj 3162 Walking Race
- poj 3162 Walking Race
- POJ 3162 Walking Race
- 解题报告:POJ 3162 Walking Race 树型DP+单调RMQ
- POJ 3162 Walking Race 笔记
- POJ 3162 Walking Race (DFS + 线段树)
- POJ 3162 Walking Race 树形DP
- POJ 3162 Walking Race (树形DP)
- ltr553 input设备
- Spring注解@ModelAttribute与@RequestBody的区别
- html页面输入点击查询跳到查询结果页面
- java Interger
- 验证日期时间格式
- POJ 3162 Walking Race (单调队列)@
- Python-helloworld
- poj-3693(后缀数组+RMQ)
- Winform加载图片
- 深入学习理解java-ThreadLocal
- 四大组件之Activity、BroadCast Recevier
- 电脑关机后电源灯还亮
- Android高级Renderscript---Andvanced Renderscript(一)
- 计算两个矩阵的乘积