原题链接:http://codeforces.com/contest/1041/problem/D
Glider
A plane is flying at a constant height of h h h meters above the ground surface. Let’s consider that it is flying from the point ( − 1 0 9 , h ) (−10^9,h) (−109,h) to the point ( 1 0 9 , h ) (10^9,h) (109,h) parallel with O x Ox Ox axis.
A glider is inside the plane, ready to start his flight at any moment (for the sake of simplicity let’s consider that he may start only when the plane’s coordinates are integers). After jumping from the plane, he will fly in the same direction as the plane, parallel to O x Ox Ox axis, covering a unit of distance every second. Naturally, he will also descend; thus his second coordinate will decrease by one unit every second.
There are ascending air flows on certain segments, each such segment is characterized by two numbers x 1 x_1 x1 and x 2 ( x 1 < x 2 ) x_2 (x_1<x_2) x2(x1<x2) representing its endpoints. No two segments share any common points. When the glider is inside one of such segments, he doesn’t descend, so his second coordinate stays the same each second. The glider still flies along O x Ox Ox axis, covering one unit of distance every second.
If the glider jumps out at 1 1 1, he will stop at 10 10 10. Otherwise, if he jumps out at 2 2 2, he will stop at 12 12 12.
Determine the maximum distance along O x Ox Ox axis from the point where the glider’s flight starts to the point where his flight ends if the glider can choose any integer coordinate to jump from the plane and start his flight. After touching the ground the glider stops altogether, so he cannot glide through an ascending airflow segment if his second coordinate is 0 0 0.
Input
The first line contains two integers n n n and h ( 1 ≤ n ≤ 2 ⋅ 1 0 5 , 1 ≤ h ≤ 1 0 9 ) h(1≤n≤2⋅10^5,1≤h≤10^9) h(1≤n≤2⋅105,1≤h≤109) — the number of ascending air flow segments and the altitude at which the plane is flying, respectively.
Each of the next n n n lines contains two integers x i 1 x_{i1} xi1 and x i 2 ( 1 ≤ x i 1 < x i 2 ≤ 1 0 9 ) x_{i2}(1≤x_{i1}<x_{i2}≤10^9) xi2(1≤xi1<xi2≤109) — the endpoints of the i i i-th ascending air flow segment. No two segments intersect, and they are given in ascending order.
Output
Print one integer — the maximum distance along O x Ox Ox axis that the glider can fly from the point where he jumps off the plane to the point where he lands if he can start his flight at any integer coordinate.
Examples
input
3 4
2 5
7 9
10 11
output
10
input
5 10
5 7
11 12
16 20
25 26
30 33
output
18
input
1 1000000000
1 1000000000
output
1999999999
Note
In the first example if the glider can jump out at ( 2 , 4 ) (2,4) (2,4), then the landing point is ( 12 , 0 ) (12,0) (12,0), so the distance is 12 − 2 = 10 12−2=10 12−2=10.
In the second example the glider can fly from ( 16 , 10 ) (16,10) (16,10) to ( 34 , 0 ) (34,0) (34,0), and the distance is 34 − 16 = 18 34−16=18 34−16=18.
In the third example the glider can fly from ( − 100 , 1000000000 ) (−100,1000000000) (−100,1000000000) to ( 1999999899 , 0 ) (1999999899,0) (1999999899,0), so the distance is 1999999899 − ( − 100 ) = 1999999999 1999999899−(−100)=1999999999 1999999899−(−100)=1999999999.
题解
首先根据贪心起点一定在上升气流的左端点,就可以搞个 t w o p o i n t e r s two\ pointers two pointers每次往右移,但是感觉贼难写,不如二分吧,直接二分飞机在哪儿落地。
代码
#include<bits/stdc++.h>
using namespace std;
const int M=2e5+5;
int le[M],ri[M],low[M],n,h,ans;
int fly(int s,int h){int pos=upper_bound(low+1,low+n+1,h+low[s-1])-low-1,d=h+low[s-1]-low[pos];return d?ri[pos+1]+d:le[pos+1];}
void in(){scanf("%d%d",&n,&h);for(int i=1;i<=n;++i)scanf("%d%d",&le[i],&ri[i]),low[i-1]=le[i]-ri[i-1];}
void ac(){low[0]=0;for(int i=2;i<n;++i)low[i]+=low[i-1];low[n]=INT_MAX;for(int i=1;i<=n;++i)ans=max(ans,fly(i,h)-le[i]);printf("%d",ans);}
int main(){in();ac();}