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;
}

Glider Gym - 101911B相关推荐

  1. B - Glider Gym - 101911B(二分)

    output standard output A plane is flying at a constant height of hh meters above the ground surface. ...

  2. B - Glider Gym - 101911B (贪心)

    A plane is flying at a constant height of hh meters above the ground surface. Let's consider that it ...

  3. Gym - 101911B Glider【尺取】

    参考大佬的博客https://blog.csdn.net/mmk27_word/article/details/85063325?ops_request_misc=%257B%2522request% ...

  4. Gym - 101911B Glider 贪心

    A plane is flying at a constant height of hh meters above the ground surface. Let's consider that it ...

  5. Gym - 101911B Glider(前缀和+二分)

    传送门:点我 A plane is flying at a constant height of hh meters above the ground surface. Let's consider ...

  6. 强化学习(三) - Gym库介绍和使用,Markov决策程序实例,动态规划决策实例

    强化学习(三) - Gym库介绍和使用,Markov决策程序实例,动态规划决策实例 1. 引言 在这个部分补充之前马尔科夫决策和动态规划部分的代码.在以后的内容我会把相关代码都附到相关内容的后面.本部 ...

  7. Gym - 102082G

    Gym - 102082G https://vjudge.net/problem/2198225/origin 对于数列中任意一个数,要么从最左边到它不递减,要么从最右边到到它不递减,为了满足这个条件 ...

  8. 安装gym库_强化学习Gym库学习实践(一)

    最近看了一篇研究方向相关的文章,介绍了一种DQN的应用,感觉还挺新鲜的.想着把这篇文章复现出来,就开始学习强化学习的相关知识,作为一名小白,这一路走的可是真的十分艰难(我太菜了啊!) 看了莫烦Pyth ...

  9. 强化学习环境库 Gym 发布首个社区发布版,全面兼容 Python 3.9

    作者:肖智清 来源:AI科技大本营 强化学习环境库Gym于2021年8月中旬迎来了首个社区志愿者维护的发布版Gym 0.19.该版本全面兼容Python 3.9,增加了多个新特性. 强化学习环境库的事 ...

最新文章

  1. java中的保留字_Java中的保留字是哪些呢?
  2. Briefings in Bioinformatics:微生物基因组学和功能基因组学相关软件和数据库的研究进展
  3. MySQL学习随笔记录
  4. python中and与or的执行顺序-python 代码运行顺序问题?
  5. Linux_查看CPU信息、机器型号等硬件信息
  6. 手动配置WCF宿主的.config文件遇到的几种错误
  7. VTK:PolyData之Stripper
  8. 计算机类高职院校课题,高职院校公共计算机类课程体系构建的教改研究
  9. Axure下拉框级联操作
  10. linux svn 面板,Linux下SVN服务器搭建(CentOS+Subversion)
  11. adb指令禁用软件_技巧 | adb助你华为手机免ROOT卸载预装软件
  12. 编程通用知识 文件流
  13. 渲染TA实战:三方向映射 UE4
  14. 判断所输入的数是否为“四叶玫瑰数”
  15. 千万同时在线直播聊天室架构演进
  16. 数字计算lisp_可从计算尺和LISP中汲取教训
  17. php是舍五入,php四舍php四舍五入五入函数 floor函数、ceil函数、round
  18. 把Redis当作队列来用,真的合适吗
  19. 华为荣耀手机复制卡号,开启NFV功能。说白了就是将原本的卡的信息复制到手机上,以后不用带卡,带手机就行了。
  20. Android开发自定义UI组件

热门文章

  1. PBI Report Server 报表页面自动刷新 2步走
  2. java版飞鸽传书源代码
  3. 备忘录_C++_拷贝构造函数
  4. Python爬虫怎么挣钱?解析Python爬虫赚钱方式,轻轻松松月入两万,再也不用为钱发愁啦
  5. android与h5交互设计,H5与native交互之JSBridge
  6. 《678辅助网SEO改善》微调网站的益处
  7. unzip解压所有zip格式
  8. 个人上传:吴恩达课后作业第四周-搭建深层神经网络(英文)
  9. 图像超分辨率重建算法,让模糊图像变清晰(附数据和代码)
  10. 软件工程常用工具列表