POJ 3162 Walking Race TreeDP+双单调队列 ★
2016-07-25 15:01
393 查看
Walking Race
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
Sample Output
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
POJ Monthly--2006.12.31, flymouse
题意:求出树上每个点到最远点的距离,构成一个序列,然后找到最长的区间,满足其中最大值与最小值之差不超过M,输出区间长度。
(N<=1e6)
1.先用树形dp求出每个点到最远点的距离。
2.维护一个单增队列和一个单减队列,可以确定当前区间内的最大最小值。
用vector保存树结构会超时。
想试试RMQ能不能做,果断爆内存。
Time Limit: 10000MS | Memory Limit: 131072K | |
Total Submissions: 3599 | Accepted: 891 | |
Case Time Limit: 3000MS |
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
POJ Monthly--2006.12.31, flymouse
题意:求出树上每个点到最远点的距离,构成一个序列,然后找到最长的区间,满足其中最大值与最小值之差不超过M,输出区间长度。
(N<=1e6)
1.先用树形dp求出每个点到最远点的距离。
2.维护一个单增队列和一个单减队列,可以确定当前区间内的最大最小值。
用vector保存树结构会超时。
想试试RMQ能不能做,果断爆内存。
#include<cstdio> #include<string> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<vector> using namespace std; #define all(x) (x).begin(), (x).end() #define for0(a, n) for (int (a) = 0; (a) < (n); (a)++) #define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++) #define LEFT(x) (x<<1) #define ysk(x) (1<<(x)) typedef long long ll; typedef pair<int, int> pii; const int INF =0x3f3f3f3f; const int maxn=1000000 ; ll lest[maxn+10],sec[maxn+10]; int plest[maxn+10],psec[maxn+10],n,m; int fir[maxn+10],nex[2*maxn+10]; int nedge; struct Edge { int to; ll w; Edge(){} Edge(int to,ll w):to(to),w(w){} }edges[2*maxn+10]; void update(int x,int y,ll val) { if( val>=lest[x]) { sec[x]=lest[x]; psec[x]=plest[x]; lest[x]=val; plest[x]=y; } else if(val>=sec[x]) { sec[x]=val; psec[x]=y; } } void dfs(int x,int fa) { lest[x]=sec[x]=0; plest[x]=psec[x]=-1; for(int i=fir[x];~i ;i=nex[i]) { Edge & e=edges[i]; int y=e.to;if(y==fa) continue; ll w=e.w; dfs( y,x); update(x,y,lest[y]+w); } } void dfs2(int x,int fa,ll dis) { if(~fa) { ll ret= plest[fa]==x?sec[fa] :lest[fa]; update(x,fa,dis+ret); } for(int i=fir[x];~i ;i=nex[i]) { Edge &e=edges[i]; int y=e.to;if(y==fa) continue; ll w=e.w; dfs2(y,x,w); } } void init() { nedge=0; memset(fir,-1,(n+1)*sizeof fir[0]); } inline void add_edge(int x,int y,ll w) { edges[nedge]=Edge(y,w); nex[nedge]=fir[x]; fir[x]=nedge++; } int qs[maxn+10],ql[maxn+10]; int solve() { int ans=0; int front_s=0,rear_s=0,front_l=0,rear_l=0; int le=1; for(int i=1;i<=n;i++) { while( front_s<rear_s&&lest[qs[rear_s-1]]>=lest[i] ) rear_s--;//单增队列,维护最小值 qs[rear_s++ ]=i; while( front_l<rear_l&&lest[ql[rear_l-1]]<=lest[i] ) rear_l--;//单减队列,维护最大值 ql[rear_l++ ]=i; while( lest[ql[front_l]]-lest[qs[front_s]]>m ) { if(qs[front_s]==le) front_s++; if(ql[front_l]==le) front_l++; le++; } ans=max(ans,i-le+1); } return ans; } int main() { int x;ll w; scanf("%d%d",&n,&m); { init(); for(int i=2;i<=n;i++) { scanf("%d%lld",&x,&w); add_edge(x,i,w); add_edge(i,x,w); } dfs(1,-1); dfs2(1,-1,0); printf("%d\n",solve()); } return 0; } /* 12 6 1 2 2 4 2 2 1 6 1 5 6 1 6 1 7 2 7 3 10 1 11 4 ans =6 */
相关文章推荐
- windows remote login linux encounter black screen
- shell常用文本工具
- Java Programming Test Question 2
- 常量
- tjut 4975
- 问题:文件显示不全,无法添加文件
- 链表
- Apache Shiro学习笔记(三)用户授权自定义Permission
- Sublime Text3: 让JSX支持Emmet调用代码补全和缩写(tab)
- 数据结构疑惑
- 阿里大鱼简单发送短信功能.net core版
- 跟我学Python系列 -Map()函数的理解
- [转]修改github已提交的用户名和邮箱
- Sublime Text3: 让JSX支持Emmet调用代码补全和缩写(tab)
- Oracle的分页查询
- css实现强制不换行/自动换行/强制换行
- struts2环境配置
- XCode 默认注释模板
- 二分查找
- PreparedStatement相较Statement的好处