【CodeForces - 1041D】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

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

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441533.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【牛客 - 185F】 假的数学游戏(斯特林公式,大数Java打表)

题干&#xff1a; 输入描述: 第一行&#xff1a;一个整数X 输出描述: 第一行&#xff1a;一个整数N 示例1 输入 复制 7 输出 复制 10 备注: 每个测试点所对应的X满足&#xff1a;第i个测试点输入的值为第i-1个测试点输入的值乘以10再加上7。特别的&#xff0c;第一个…

*【HDU - 6333】Problem B. Harvest of Apples (莫队,逆元,组合数学)(这样预处理正确吗?)

题干&#xff1a; There are nn apples on a tree, numbered from 11 to nn. Count the number of ways to pick at most mm apples. Input The first line of the input contains an integer TT (1≤T≤105)(1≤T≤105) denoting the number of test cases. Each test c…

ruoyi 多模块部署_大数据时代,独立部署的商城系统具有哪些优势?

独立部署是把一个可部署软件包安装到一个指定IT环境上并让其按预定流程提供服务的过程。现如今&#xff0c;越来越多的商家开始搭建网上商城系统&#xff0c;从而为自己积攒多年的客户群体提供更为便捷的购物模式&#xff0c;让购物变得更加智能化。独立部署的商城系统具有哪些…

【51nod - 1098】 最小方差(基础数学,公式化简,前缀和,积的前缀和)

题干&#xff1a; 若x1,x2,x3……xn的平均数为k。 则方差s^2 1/n * [(x1-k)^2(x2-k)^2…….(xn-k)^2] 。 方差即偏离平方的均值&#xff0c;称为标准差或均方差&#xff0c;方差描述波动程度。 给出M个数&#xff0c;从中找出N个数&#xff0c;使这N个数方差最小。 Input …

牧马人机械鼠标g3_性价比好的有线鼠标都有哪些?2020年12款热选游戏鼠标推荐...

决定一款鼠标好坏因素众多&#xff0c;我们常常发现一款鼠标&#xff0c;有些用户觉得非常好&#xff0c;也有些用户觉得不好&#xff0c;是因为大多数用户对于鼠标的尺寸大小、轻重、实际的手感等方面都会有所不同&#xff0c;这都很正常&#xff0c;所以适合自己才是最好的。…

emc re 整改 超标_老刘工程师睡前故事5-EMC 辐射发射超标怎么办?

老刘工程师睡前故事-EMC 辐射发射超标怎么办&#xff1f;今天的工程师睡前故事讲讲汽车EMC测试中辐射发射超标了怎么办?首先来聊聊辐射发射的基本知识辐射发射英文简称RE&#xff0c;检测的是产品对外的辐射干扰。国际标准参考CISPR25&#xff0c;国标参考GBT18655。这个标准分…

【PAT - 甲级 - 1018】Public Bike Management (带权最短路,多条最短路中加条件,DFS)

题干&#xff1a; 链接&#xff1a;https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f 来源&#xff1a;牛客网 There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. On…

【牛客 - 181C】序列(前缀和,二分,可用set维护)(有坑)

题干&#xff1a; 小a有n个数&#xff0c;他想把他们划分为连续的权值相等的k段&#xff0c;但他不知道这是否可行。 每个数都必须被划分 这个问题对他来说太难了&#xff0c;于是他把这个问题丢给了你。 输入描述: 第一行为两个整数n,q&#xff0c;分别表示序列长度和询问…

c/c++,字符,字符串,各种方式读入与对空格,回车的处理

#include<iostream> #include<string> using namespace std; int main() {char a[50],b[50],charr;//经测试&#xff0c;cin读入字符串&#xff0c;会识别空格和回车为截止&#xff0c;并且不会吞掉&#xff0c;//只是每次读的时候会从第一个不为空格/回车的字符开…

【CodeForces - 357D】Xenia and Hamming (字符串问题,数论,思维)

题干&#xff1a; Xenia is an amateur programmer. Today on the IT lesson she learned about the Hamming distance. The Hamming distance between two strings s  s1s2... sn and t  t1t2... tn of equal length n is value . Record [si ≠ ti] is the Iverson n…

【CodeForces - 371D】Vessels(思维,元素合并,并查集)

题干&#xff1a; There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is ai liters. Initially…

code iban 是有什么组成_EAN-128码和Code-128码的区别

什么是Code-128码&#xff1f;什么是EAN-128码&#xff1f;二者之间有什么区别&#xff1f;接下来小编就给大家解除心中的疑惑。Code-128码是一种高密度的条形码&#xff0c;可表示从 ASCII 0 到ASCII 127 共128个字符&#xff08;其中包含数字&#xff0c;字母&#xff0c;符号…

计算机中丢失setupxml.dll,Win7电脑安装VideoStudio Pro X6显示丢失SetupXML.dll文件怎么解决...

最近有win7系统用户在电脑安装VideoStudio Pro X6软件的时候&#xff0c;突然出现错误的提示&#xff0c;显示无法启动此程序&#xff0c;因为计算机中丢失SetupXML.dll。尝试重新安装该程序来解决此问题&#xff0c;要怎么办呢&#xff0c;下面给大家讲解一下Win7电脑安装软件…

怎么p出模糊的照片_36. 盲去卷积 - 更加实用的图像去模糊方法

本文同步发表在我的微信公众号和知乎专栏“计算摄影学”&#xff0c;欢迎扫码关注&#xff0c;上一篇文章35. 去卷积&#xff1a;怎么把模糊的图像变清晰&#xff1f;吸引了很多朋友的关注。在这篇文章里面&#xff0c;我给大家讲了一种叫做“非盲去卷积”的方法&#xff0c;当…

docker修改镜像的存储位置_云原生存储详解:容器存储与 K8s 存储卷(内含赠书福利)...

作者 | 阚俊宝 阿里巴巴技术专家参与文末留言互动&#xff0c;即有机会获得赠书福利&#xff01;导读&#xff1a;云原生存储详解系列文章将从云原生存储服务的概念、特点、需求、原理、使用及案例等方面&#xff0c;和大家一起探讨云原生存储技术新的机遇与挑战。本文为该系列…

计算机专业用锐龙笔记本,轻松应对工作挑战——ThinkPad T14 锐龙版,适合办公的笔记本电脑...

拥有一部适合办公的笔记本电脑&#xff0c;可以成为商务人士忙碌工作中强有力的支持。联想旗下的ThinkPad 系列笔记本电脑&#xff0c;一直秉持为高端商务人士服务的理念&#xff0c;以稳定、流畅、安全的使用体验得到广泛认可。其中的ThinkPad T14 锐龙版&#xff0c;更是有着…

python tabula 使用方法_Python中os.walk()的使用方法

os.walk()主要用来扫描某个指定目录下所包含的子目录和文件。这篇文章将通过几个简单的例子来说明python中os.walk()的使用方法。假设我们的test文件夹有如下的目录结构&#xff1a;我们首先用os.walk扫描test文件夹下所有的子目录和文件&#xff1a;# 使用os.walk扫描目录 imp…

乐乐勇智能教育机器人有多少型号_【头条】协作机器人平台化趋势将会是柔性自动化的破局之道...

智能机器人商情微信公众号&#xff0c;关注中国智能机器人行业热点与发展趋势&#xff0c;打造快捷高效的行业资讯交互平台。更多精彩内容&#xff0c;您可以点击标题下方的蓝字关注我们。导语艾利特平台级CS系列协作机器人全新发布9月15日上海工博会第一天&#xff0c;艾利特机…

计算机毕设结束语致谢,毕业设计结束语和致谢

毕业设计结束语和致谢时间&#xff1a;2021-01-17 20:08:40 分类&#xff1a;小结范文 | 毕业论文致谢词范文 | Word文档下载毕业设计结束语和致谢导语&#xff1a;论文致谢应以简短的文字对课题研究与论文撰写过程中间直接给予帮助的人员(例如指导教师、答疑教师及其他人员)…

CSS中属性个属性值怎么区分,[CSS] 详细解释 @media 属性与 (max

前言现在 HTML5/CSS3 很流行罢&#xff0c;也是未来时代的趋势。在 HTML5 带来的许多实用功能之后&#xff0c;CSS3也同带来了一些牛逼哄哄的功能呢。动画 animation转化 transform过渡 translation尽快这已足够让我们兴奋&#xff0c;许多之前必须用 JS 或 JQ 写的效果用 CSS …