Codeforces Round #509 (Div. 2) - D - Glider(枚举递推)

27 篇文章 0 订阅
13 篇文章 0 订阅

Codeforces Round #509 (Div. 2) - D - Glider

题意:

已知有 n 个有上升气流的区域(不相交且按递增序给出),求用滑翔机从高度h出发,最远能飞多远。

在上升气流区域中不会下降,否则每飞一米高度下降一米。

可以在任意横坐标跳机,跳机时高度为h。

落地后不能再往前飞,就算是上升气流也不行。

可以知道在上升气流刚开始的位置跳机最好。

枚举所有的跳机位置,得到对应的答案,取最大值。

对于第一个答案可以直接跑下去。

对于下一个答案可以从上一个推下来,因为中间一部分的过程是一样的,只要将整体上移两个上升气流的距离然后再继续下推即可。

要注意的是上移后总高度不能大于h,以及当相邻两个上升气流距离超过 h 则延续性就会被打断,此时重新开始计算。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <queue>
#include <algorithm>
#include <functional>
#include <map>
#include <iomanip>
#include <vector>
#include <set>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
LL n, h, x1[N], x2[N];

int main()
{
    scanf("%lld%lld", &n, &h);
    LL ans = h, x = 0, y = h, ed = 1;
    for(int i=1;i<=n;i++) scanf("%lld%lld", &x1[i], &x2[i]), ans = max(ans, x2[i]-x1[i] + h);
    for(int i=1;i<=n;i++) {
        if(x1[i] - x2[i-1] >= h) ed = i;
        y += x1[i] - x2[i-1];
        y = min(y, h);
        for(int j=ed+1;j<=n;j++) {
            if(x1[j] - x2[j-1] >= y) {
                ed = j - 1;
                break;
            }else y -= x1[j] - x2[j-1], ed = j;
        }
        if(x2[ed] - x1[i] + y > ans) ans = x2[ed] - x1[i] + y;
    }
    printf("%lld\n", ans);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值