Glider Gym - 101911B

A plane is flying at a constant height of h meters above the ground surface. Let’s consider that it is flying from the point (−109,h) to the point (109,h) parallel with 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 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 x1 and 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 Ox axis, covering one unit of distance every second.

在这里插入图片描述

If the glider jumps out at 1, he will stop at 10. Otherwise, if he jumps out at 2, he will stop at 12.
Determine the maximum distance along 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.

Input
The first line contains two integers nn and hh (1≤n≤2⋅105,1≤h≤109)(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 nn lines contains two integers xi1xi1 and xi2xi2 (1≤xi1<xi2≤109)(1≤xi1<xi2≤109) — the endpoints of the ii-th ascending air flow segment. No two segments intersect, and they are given in ascending order.

Output
Print one integer — the maximum distance along OxOx 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), then the landing point is (12,0)(12,0), so the distance is 12−2=1012−2=10.

In the second example the glider can fly from (16,10)(16,10) to (34,0)(34,0), and the distance is 34−16=1834−16=18.

In the third example the glider can fly from (−100,1000000000)(−100,1000000000) to (1999999899,0)(1999999899,0), so the distance is 1999999899−(−100)=19999999991999999899−(−100)=1999999999.

题意:你在高度为h的地方,每秒y坐标会减少1,x坐标会增加1,现在会n个上升气流区间[l,r],在每个区间中,你的y坐标不会改变,你的x坐标每秒会增加1。你可以从x轴上的任意一点下落,现在问你最远的飞行路径。

思路:有一个不变的地方,就是下落的高度一定是h,可以转化为经过总长度为h的间隙,尽可能地选择经过更长的上升气流区间。如何维护这个总长度h呢?我们可以用前缀和sum1[i]表示前i个间隙的总长度,用前缀和sum2[i]表示前i个上升气流的总长度。我们用二分查找第一个大于等于sum1[i]+h的点的位置pos,则sum2[pos-1]-sum2[i-1]为经过的当我们下降h时经过的上升气流总长度,我们维护一个最大值maxx,结果即为maxx+h。

总结:这道题对我的启发很大,要好好利用前缀和+二分来解决问题

#include<iostream>
#include<algorithm>
using namespace std;
struct node{
	int l;
	int r;
}x[200009];
int a[200009],b[200009];
int main()
{
	int n,h;
	cin>>n>>h;
	for(int i=1;i<=n;i++)
		cin>>x[i].l>>x[i].r;
	int maxx=-1;
	a[1]=x[1].r-x[1].l;b[1]=0;
	for(int i=2;i<=n;i++)
	{
		a[i]=a[i-1]+x[i].r-x[i].l;
		b[i]=b[i-1]+x[i].l-x[i-1].r;
	}
	b[n+1]=0x3f3f3f3f;
	for(int i=1;i<=n;i++)
	{
		int pos=lower_bound(b+1,b+1+n,b[i]+h)-b;
		maxx=max(maxx,a[pos-1]-a[i-1]);
	}
	cout<<maxx+h<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/why932346109/article/details/85137289