像这样细节老是打挂不行啊……

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 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) — 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.


题目大意

二维坐标系里有一些蓝色区域和白色区域。在蓝色区域中飞行高度不会降低;在白色区域中飞行每个单位会降低一个单位。

请找出一条路径使得水平飞行距离最远。

题目分析

可以看成是这个东西:

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

于是做法还是挺显然的,因为对于每个起点,选取的[l,r]是单调的。

关键在于边界条件的小细节……今天做的时候WA了两发。

 1 #include<bits/stdc++.h>
 2 const int maxn = 200035;
 3
 4 int n,h,l,r,lim,ans,cnt;
 5 int ls[maxn],rs[maxn];
 6 int w[maxn],c[maxn];
 7
 8 int read()
 9 {
10     char ch = getchar();
11     int num = 0;
12     bool fl = 0;
13     for (; !isdigit(ch); ch = getchar())
14         if (ch=='-') fl = 1;
15     for (; isdigit(ch); ch = getchar())
16         num = (num<<1)+(num<<3)+ch-48;
17     if (fl) num = -num;
18     return num;
19 }
20 int main()
21 {
22     n = read(), h = read();
23     for (int i=1; i<=n; i++) ls[i] = read(), rs[i] = read();
24     for (int i=1; i<=n; i++)
25         w[i] = rs[i]-ls[i], c[i] = ls[i+1]-rs[i];
26     r = 1, cnt = ans = w[1];
27     for (int i=1; i<=n; i++)
28     {
29         while (r<n&&lim+c[r]<h) lim += c[r++], cnt += w[r];
30         ans = std::max(ans, cnt);
31         if (r!=i) lim -= c[i];        //向前推一格
32         else cnt += w[i+1], r = i+1;    //间隙太大,置零重开
33         cnt -= w[i];    //减去当前位置
34     }
35     printf("%d\n",ans+h);
36     return 0;
37 }

END

转载于:https://www.cnblogs.com/antiquality/p/9693523.html

【two pointers 细节题】cf1041dD. Glider相关推荐

  1. [ 习题 ] 句子简化题 细节题 排除题

    [ 句子简化题例题讲解 ] [ 例题讲解 1 ] TPO-15 A Warm-Blooded Turtle In a countercurrent exchange system, the blood ...

  2. [ 2204阅读 ] 句子简化题 | 细节题 | 排除题 | 推理题 | 目的题 | 句子插入题 | 总结题

    任务:课前资料01理论课.03句子分析(每天一个).04语法课.02词汇题(324每天30) [ 句子简化题 ] - [ 细节题 ] - [ 排除题 ] - [ 推理题 ] - [ 目的题 ] - [ ...

  3. 考研英语阅读理解做题技巧(6):细节题

    细节题:必考:分值特别高 1.出题形式:看题目 1)题目是以疑问词开头的:what;why;when...,除了主旨题:态度题:推断题 which of the following is true?判 ...

  4. Educational Codeforces Round 98 (Rated for Div. 2) E. Two Editorials 细节题

    题目连接 真细节题- 选了一个很复杂的做法. 枚举第一个作者AAA选的框,枚举选手iii.你会发现另一个作者BBB选的框对于选手iii来说,会有一些右端点连续的区间比AAA更优.那这样就可以知道作者B ...

  5. ZCMU-1635:超大型 LED 显示屏(细节题)

    Problem A: 超大型 LED 显示屏 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 71  Solved: 40 [Submit][Statu ...

  6. 2019 ICPC全国邀请赛(西安)I. Cracking Password(序列检验,BSGS,细节题)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 2019 ICPC全国邀请赛(西安)I. Cracking Password Weblink http ...

  7. Python稳基修炼之计算机等级考试易错细节题4(含答案)

    61.返回一个代表时间的精确浮点数,两次或多次调用,其差值用来计时,这个函数是 A.time.perf_counter( B.time.mktime(t) C.time.strftime(format ...

  8. Python稳基修炼之计算机等级考试易错细节题3(含答案)

    41. Python数据可视化方向的第3方库是 A.Panda3d B.cocos2d C.mayavi D.Pyramid 答案:C 42. Python图形用户界面方向的第三方库是 A.openp ...

  9. Python稳基修炼之计算机等级考试易错细节题2(含答案和解析)

    21.以下程序的输出结果是_ s="LOVES" print("{:^13}" .format(S)) A.LOVES B.******** LOVES C.L ...

最新文章

  1. 【C++】bind参数绑定 P354(通用的函数适配器)
  2. IBM斥资3.6亿美元建史上最复杂云计算中心
  3. 【RESTful风格】软件接口设计中RESTful风格
  4. C# WPF MVVM项目实战(进阶②)
  5. poj 1018 Communication System
  6. TensorFlow2.0(三)--Keras构建神经网络回归模型
  7. linux input输入子系统分析《四》:input子系统整体流程全面分析
  8. 代码管理学:域名、产品名、应用名等都不是你能决定的,要上报备案
  9. LOJ#6038. 「雅礼集训 2017 Day5」远行(LCT)
  10. biopython有什么用_用Biopython解析BLAST结果
  11. PMI采购MogaFX经理人指数是什么
  12. LoadRunner 录制IE 8卡死
  13. 企业微信如何给客户打标签?
  14. 2020平面设计新手入门技巧
  15. MySQL充电_mysql语法充电
  16. 蓝桥杯历年试题汇总 C/C++ B组
  17. QT学习之路-资料收藏集锦
  18. ISeeYou:一款基于Bash和Javascript开发的社会工程学网络钓鱼工具
  19. 教程制作者角度的教程炼狱
  20. 预制体与非预制体_为什么更多的Web设计师应该尝试使用预制网站

热门文章

  1. 微软发布 PowerToys 0.12.0,带来批量处理工具和暗黑模式
  2. python编程可以做什么菜_Python 编程! 我是菜菜菜鸟 大家帮帮忙
  3. php转go注意,PHP转Golang一些感想
  4. transformer中attention计算方式_Reformer: 局部敏感哈希、可逆残差和分块计算带来的高效...
  5. 在java中获取当前时间_JAVA中获取当前系统时间
  6. eclipse怎么运行不了服务器,eclipse可以运行,部署服务器启动不起来
  7. linux e1000e 网卡驱动,在centos7上安装intel e1000e 网卡驱动
  8. 愤怒的小鸟的html制作,如何做html5山寨版愤怒的小鸟
  9. linux r后台执行,screen 命令简单用法 Linux后台执行 就用它
  10. go 判断元素是否在slice_golang 判断 两个slice 是否相等