Gym - 101911B Glider 贪心

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

 If the glider jumps out at 11, he will stop at 1010. Otherwise, if he jumps out at 22, he will stop at 1212.

Determine the maximum distance along OxOx 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 00.

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

题意:给出的是n个气流带,现在所在的高度h。在气流带中不会下降,不在气流带则会每次下降一个单位。注意如果高度为0,恰好到下一个气流带的起点,也不能经过下一个加速带。起点的x值可以任你选择,最后降落下来到地面的点,离你选择起点的最远距离是多少。

题解:很明显我们应该从某个气流带的起点开始,因为这样能保证在任意时刻所处的高度最高,然后我们就贪心就可以了,枚举起点即可,然后二分找中点即可,这样复杂的nlog(n),但我们这里介绍一个O(n)的算法,就是贪心就好了,先固定起点然后不断向后找终点,若高度不符合了,就把起点往后移就可以了,这样2*n 即可解决

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
struct node{
	int l,r;
}a[N];
int n,h;
int main()
{
	while(~scanf("%d%d",&n,&h))
	{
		for(int i=1;i<=n;i++)scanf("%d%d",&a[i].l,&a[i].r);
		int l=1,r=1;
		int ans=0;
		int dh=0,cnt=0;
		while(r<=n)
		{
			if(r>1)
			{
				dh+=a[r].l-a[r-1].r;
				while(dh>=h)
				{
					dh-=a[l+1].l-a[l].r;
					cnt-=a[l].r-a[l].l;
					l++;
				}
			}
			cnt+=a[r].r-a[r].l;
			r++;
			ans=max(ans,cnt);
		}
		printf("%d\n",ans+h);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值