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