【CF1041D】 Glider

题目

题意翻译
你在玩一个吃鸡游戏,你现在要跳伞。你的飞机现在在高度为 h h h的空中飞行,你每飞一个单位长度的距离,你就会下落一个单位长度的高度,当然,有些地方是上升气流,你不会下落,你会往前直飞,由于你想在空中就被人打死,求你最远的飞行距离

输入格式:
第一行两个正整数 n n n h h h,代表有 n n n段上升气流,飞机的高度为 h h h

接下来 n n n行,每行两个数 x i 1 x_{i1} xi1 x i 2 x_{i2} xi2。代表 x i 1 x_{i1} xi1 x i 2 x_{i2} xi2这段区间为上升气流。

输出格式:
一个整数,代表你最远的飞行距离
题目描述
A plane is flying at a constant height of h h meters above the ground surface. Let’s consider that it is flying from the point (-10^9, h) (−10
9
,h) to the point (10^9, h) (10
9
,h) parallel with Ox 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 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 x_1 x
1
​ and x_2 x
2
​ ( x_1 < x_2 x
1
​ <x
2
​ ) 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 Ox axis, covering one unit of distance every second.

If the glider jumps out at 1 1 , he will stop at 10 10 . Otherwise, if he jumps out at 2 2 , he will stop at 12 12 .Determine the maximum distance along Ox 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 0 .

输入输出格式
输入格式:
The first line contains two integers n n and h h (1 \le n \le 2\cdot10^{5}, 1 \le h \le 10^{9}) (1≤n≤2⋅10
5
,1≤h≤10
9
) — the number of ascending air flow segments and the altitude at which the plane is flying, respectively.

Each of the next n n lines contains two integers x_{i1} x
i1
​ and x_{i2} x
i2
​ (1 \le x_{i1} < x_{i2} \le 10^{9}) (1≤x
i1
​ <x
i2
​ ≤10
9
) — the endpoints of the i i -th ascending air flow segment. No two segments intersect, and they are given in ascending order.

输出格式:
Print one integer — the maximum distance along Ox Ox 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.

输入输出样例
输入样例#1: 复制
3 4
2 5
7 9
10 11
输出样例#1: 复制
10
输入样例#2: 复制
5 10
5 7
11 12
16 20
25 26
30 33
输出样例#2: 复制
18
输入样例#3: 复制
1 1000000000
1 1000000000
输出样例#3: 复制
1999999999
说明
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 = 10 12−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=18 34−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)=1999999999 1999999899−(−100)=1999999999 .

思路

用两个指针分别指开始和结束位置
根据贪心的思想,出发点一定在某个上升气流的左端点
首先处理l=1的情况
我在代码中用了一个now记录当前高度
如果从第一个区间出发可以通过最后一个区间,那么不用说,这显然最优
否则的话我们记录下滑翔机最远能到达的区间y(无法到达y+1)
此时让now记录刚通过y区间时的高度
这样我们就可以逐步将区间向右移动了
我们每次移动直接将起点x移到x+1区间的左端点
这样的话当我们到达y区间右端是就能比之前高l[x+1]-r[x]
然后继续向右更新y
注意当y==n时,以后的状态就没有必要继续更新了,因为显然我们通过的区间总长度越长答案越优,以后的右移x只会减少区间。

代码

#include<iostream>
#include<cstdio>
using namespace std;
#define maxn 200010
int n,h,ans,l[maxn],r[maxn],d[maxn];

int main()
{
    scanf("%d%d",&n,&h);
    for(int i=1;i<=n;++i)scanf("%d%d",l+i,r+i),d[i]=d[i-1]+l[i]-r[i-1];
    for(int i=1;i<=n;++i)
    {
        int id=lower_bound(d+i+1,d+n+1,h+d[i])-d-1;
        ans=max(ans,r[id]-l[i]+h-d[id]+d[i]);
    }
    printf("%d\n",ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值