题干:

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

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轴上的任意一点下落,现在问你最远的飞行路径。(即终点x坐标 减 起点x坐标的最大值)

解题报告:

大体:看到数据量,nlogn解决,于是乎枚举每一个起点,二分找到终点,维护最大值,就可以了呀。

仔细分析:

首先要分析出,把竖直下落的高度转换成走过的间隙的长度和(高中物理题貌似这种转化套路很多见?)然后我们看在这些个间隙下,能走多长个气流(不是越多越好,而是越长越好),维护一个maxx,然后最后答案就是maxx + h就可以了。

不明白可以看下面的图:

可以看成是这个东西:

相当于是在选取的ci≤h的情况下,使Σwi最大。

AC代码:

#include<bits/stdc++.h>using namespace std;
const int MAX = 2e5 + 5;
int n,h;
int b[MAX],c[MAX];
struct Node {int l,r;
} node[MAX];
int main()
{cin>>n>>h;for(int i = 1; i<=n; i++) scanf("%d%d",&node[i].l,&node[i].r);c[1] = 0;b[1] = node[1].r-node[1].l;for(int i = 2; i<=n; i++) {b[i] = node[i].r - node[i].l;c[i] = node[i].l - node[i-1].r;b[i] += b[i-1];c[i] += c[i-1];}c[n+1] = INT_MAX;//这句貌似可以没有、、int maxx = -1,ans;for(int i = 1; i<=n; i++) {int pos = lower_bound(c+1,c+n+1,c[i]+h) - c;ans = b[pos-1] - b[i-1];maxx = max(maxx,ans);}printf("%d\n",maxx+h);return 0 ;
}
/*
2 3
1 2
3 4
*/

总结:

就是要多逼着自己做做D题啊!!!有的时候也不算很难想,不是高不可攀,是不是?

关于初始化:这里对于i=1的情况单独做了初始化,然后从i=2开始进入for循环,是正解,但是不加初始化,直接i=1开始计入for,也可以ac,但是就说不太过去,反正输出c[2]的值是和我们想得到的值是不一样的,换句话说,是不合法的。

记住一个套路:

求前缀和 + 二分。其中二分寻值时可以依附于正在枚举的i上,就像这个题,二分找的就是c[i]相关的。这样就避免了很麻烦的双指针:链接在此

#include <bits/stdc++.h>
#define maxn 200005
using namespace std;
struct Node{int l,r;
}q[maxn];
typedef long long ll;
int main()
{ll n,h;cin>>n>>h;for(int i=0;i<n;i++){scanf("%I64d%I64d",&q[i].l,&q[i].r);}ll l,r,tmp1,tmp2;l=q[0].l,r=q[0].r+h;//首先l指针先指向第一个气流的起点,r指针指向落点ll ans=h;tmp1=tmp2=0;while(tmp1<n){//枚举每一个气流while(tmp2+1<n&&q[tmp2+1].l<r){//如果下一个气流的左区间在终点前,则证明出现情况2,则将终点加上该气流的区间大小tmp2++;r+=q[tmp2].r-q[tmp2].l;}ans=max(ans,r-l);//记录r-l的最大值tmp1++;l=q[tmp1].l;//将起点置为下一个气流的起点r+=q[tmp1].l-q[tmp1-1].r;   //加上气流与气流之间的大小}cout<<ans<<endl;
}

或者一位红名大佬的双指针:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define mp make_pair
const int N = 200200;
int n, h;
int a[N][2];
int ans;
int main()
{scanf("%d%d", &n, &h);for (int i = 0; i < n; i++) scanf("%d%d", &a[i][0], &a[i][1]);int r = 0;int curH = h;for (int l = 0; l < n; l++) {while(r < n - 1 && curH > a[r + 1][0] - a[r][1]) {curH -= a[r + 1][0] - a[r][1];r++;}ans = max(ans, a[r][1] - a[l][0] + curH);if (l < n - 1)curH += a[l + 1][0] - a[l][1];}printf("%d\n", ans);return 0;
}

或者app大佬:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 200000;
int n, h, l[maxn + 10], r[maxn + 10], m;
pair<ll, ll> a[maxn + 10];
int idl = 1, idr = 1;
ll lenl = -1e18, lenr = -1e18, ans;
int main() {scanf("%d%d", &n, &h);lenr += h; ans = h;for (int i = 1; i <= n; ++i) scanf("%d%d", &l[i], &r[i]);a[++m] = make_pair(-(long long)1e18, l[1]);for (int i = 2; i <= n; ++i)a[++m] = make_pair(r[i - 1], l[i]);a[++m] = make_pair(r[n], (long long)1e18);while (idl <= m && idr <= m) {ans = max(ans, lenr - lenl);ll dist = min(a[idr].second - lenr, a[idl].second - lenl);lenl += dist; lenr += dist;if (lenl < a[idl].second) ++lenl;else lenl = a[++idl].first + 1;if (lenr < a[idr].second) ++lenr;else lenr = a[++idr].first + 1;}printf("%lld", ans);
}

【CodeForces - 1041D】Glider (枚举起点,双指针 或 二分终点,思维)(知识点总结)相关推荐

  1. CodeForces 1041D Glider 枚举+二分

    题目链接:https://vjudge.net/problem/CodeForces-1041D/origin 题意: 飞行员在区间[-1e9,1e9]内(这个一定注意!!!),从高度为k处跳伞,会向 ...

  2. codeforces 1041D Glider

    题目链接:http://codeforces.com/problemset/problem/1041/D 题目简单说明:就是给定一个高度,然后有人从飞机跳下,通过滑翔机让自己每秒向右移动一个单位同时向 ...

  3. 1041D - Glider(思维+二分+前缀和)

    https://codeforces.com/problemset/problem/1041/D 思路: 可以发现,选择的起点在当前某个线段区间的左端点一定不会更差. 于是On枚举起点位置,快速找到其 ...

  4. codeforces(D2. Coffee and Coursework (Hard Version))二分答案

    这题一看很容易想到二分,但我一开始想偏了,我是想枚举天数,然后二分去验证天数是否满足,但是这样二分验证天数是否满足这一步卡住了,写不出来. 然后我改成二分天数,验证天数是否满足这一步改成暴力计算,这样 ...

  5. 2022-04-26:给定四个整数 sx , sy ,tx 和 ty,如果通过一系列的转换可以从起点 (sx, sy) 到达终点 (tx, ty),则返回 true,否则返回 false。 从点 (x

    2022-04-26:给定四个整数 sx , sy ,tx 和 ty,如果通过一系列的转换可以从起点 (sx, sy) 到达终点 (tx, ty),则返回 true,否则返回 false. 从点 (x ...

  6. 一种基于肌电信号运动起点、波峰、终点实时自动检测的方法

    一种基于肌电信号运动起点.波峰.终点实时自动检测的方法 (⊙o⊙)-,这篇是我写收费文章的第一篇.咱也尝试下知识付费,哈哈. 先看下效果,在给定理想正弦波的情况下,可以准确识别到正弦波的起点.波峰和终 ...

  7. R语言使用rnorm函数生成正太分布数据、使用plot函数可视化折线图、使用arrows函数在可视化图像中绘制箭头曲线、绘制带箭头线段,可以设置箭头角度,有几个箭头(1起点箭头、2终点箭头,3双箭头)

    R语言使用rnorm函数生成正太分布数据.使用plot函数可视化折线图.使用arrows函数在可视化图像中绘制箭头曲线.绘制带箭头线段,可以设置箭头角度,有几个箭头(1起点箭头.2终点箭头,3双箭头) ...

  8. Codeforces Round #509 D - Glider(枚举二分前缀和)

    题意: 给出滑翔机的高度h 以及有n段上升气流区间,滑翔机在非上升气流区间会以每前进一个单位 高度就下降一个单位的 速度坠落 ,而在有上升气流的区间,滑翔机将持续前进,并不会下落,问在一维坐标上的哪个 ...

  9. Glider CodeForces - 1041D

    http://codeforces.com/contest/1041/problem/D 先合并气流 得k个互不相交的气流 然后对(i-1)个空隙做前缀和 枚举以每个气流为起点的 可以飞到哪里 二分实 ...

最新文章

  1. 传百度要与阿里、腾讯争夺在线办公市场?“百度Hi”开放520人同时在线音视频会议
  2. Ueditor的两种定制方式
  3. 没有 5G 版 iPhone 的苹果秋季发布会,发布了些什么?
  4. 李飞飞点赞的NeurIPS新赛道,刚刚公布了第一批数据集benchmark入围名单
  5. python爬取分页数据
  6. 斐波那契数的两种求法(迭代,递归)
  7. 对acm icpc 的随笔——01
  8. JAVA多线程总结(笔记)
  9. java异常处理机制_Java的异常处理机制
  10. 《大型网站技术架构:核心原理与案例分析》-- 读书笔记 (2) : 大型网站核心架构要素(5) -- 安全性...
  11. 刚装的系统没有sql server(mssqlserver)_数据库与SQL学习
  12. 2020级C语言大作业 - 丛林大作战
  13. 带网格的_雨花区井圭路社区开展消防安全网格化实战演练活动
  14. springboot之整合mybatis
  15. python queue pip
  16. SAP OLE中常用的一些方法和属性
  17. 结构体(struct)的不同写法和tag前缀
  18. php 查询access数据库操作,php操作access数据库的方法详解
  19. easyui框架的使用,定制日历控件
  20. 在线教育逆流而上,网络直播课成教育新宠

热门文章

  1. 废粉盒在哪里_很想知道打印机废粉盒中的那些废碳粉应该怎么处理?
  2. 修改小程序swiper 点的样式_高质量的微信小程序样式模板应该长什么样?
  3. python判断字母数字_Python判断字符串是否为字母或者数字(浮点数)的多种方法
  4. C# WinForm中获取当前程序运行目录的方法
  5. python解释器的提示符是shell嘛_python解释器怎么运行
  6. STL内嵌数据类型: value_type
  7. ubuntu/wireshark --Lua: Error during loading: [string /usr/share/wireshark/init.lua]:45问题解决
  8. ubuntu下安装opensips
  9. Linux I2C核心、总线与设备驱动(一)
  10. linspace函数matlab_从零开始的matlab学习笔记——(29)泰勒逼近函数