题目链接:https://vjudge.net/problem/CodeForces-1041D/origin
题意:
飞行员在区间[-1e9,1e9]内(这个一定注意!!!),从高度为k处跳伞,会向右移动。
如果遇到上升气流,会水平向右移动,否则向右下方移动。
给定n端上升气流区间,不交叉不重叠。
问你最多移动多远。
先说整体思路,再说小细节。
观察可得,一般情况下,一定是从上升气流区间的左端点开始的,因为这样可以获得最大。
那么我们考虑枚举起点i(我们把上升区间和它右边的间隔区间看成一个整体,从1~n枚举),再用lower_bound查找第一个大于等于k的位置p。那么此时所移动的最大距离就是i到p的所有上升气流区间之和加上高度k(注意不是加上间隔区间之和!)。
我一开始感觉这样似乎就可以了,但是有几个问题没考虑到:
1、如果从某个起点i出发,到达边界1e9时,高度还没有降低为0,那么还可以看看i之前的区间能否再让它飞一段。所以先判断当前的k值大于剩下的间隔区间之和时,就往前找,并且不用lower_bound(因为找不到!)。
2、我们有n个上升气流区间,每个右边有一个间隔区间。如果考虑第一条的情况(详见样例3),就会出现bug,所以我们把第0个的上升区间看作[-1e9,-1e9],间隔区间为[-1e9,r],其中r为第一个上升区间的左端点。而且最后一个区间的右端点设置为[l,1e9],l为左端点
综上,这样的话就可以处理了。

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
#define inf 0x3f3f3f3f
#define sd(a) scanf("%d",&a)
#define sdd(a,b) scanf("%lld%lld",&a,&b)
#define cl(a,b) memset(a,b,sizeof(a))
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define dbg() printf("aaa\n")
using namespace std;
const int maxn=2e5+10;
ll n,k;
struct node{//a是上升气流区间 b是空白区间ll l,r;
}a[maxn],b[maxn];
ll a1[maxn],b1[maxn];
int main() {sdd(n,k);a[0].l=-1000000000;a[0].r=-1000000000;a1[0]=0;rep(i,1,n){sdd(a[i].l,a[i].r);a1[i]=a[i].r-a[i].l;}b[0].l=-1000000000;b[0].r=a[1].l;b1[0]=b[0].r-b[0].l;rep(i,1,n-1){b[i].l=a[i].r;b[i].r=a[i+1].l;b1[i]=b[i].r-b[i].l;}b[n].l=a[n].r;b[n].r=1000000000;b1[n]=b[n].r-b[n].l;rep(i,1,n){a1[i]+=a1[i-1];b1[i]+=b1[i-1];}ll maxx=-1;rep(i,1,n){//枚举从第几个白色开始if(k>b1[n]-b1[i-1]){//若能飞过界ll now=b1[n]-b1[i-1]+a1[n]-a1[i-1];ll kk=k-(b1[n]-b1[i-1]);for(int j=i-1;j>=0;j--){if(kk<=b1[j]){now+=kk;break;}else{now+=b1[j]+a1[j];kk-=b1[j];}}maxx=max(maxx,now);break;}else{//k<b1[n]-b1[i-1]//注意这里做了修正!ll p=lower_bound(b1+i,b1+n+1,k+b1[i-1])-b1;maxx=max(maxx,a1[p]-a1[i-1]+k);}}printf("%lld\n",maxx);return 0;
}

A plane is flying at a constant height of h meters above the ground surface. Let’s consider that it is flying from the point (−109,h) to the point (109,h) parallel with 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 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 x1 and x2 (x1<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 Ox axis, covering one unit of distance every second.

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

Input
The first line contains two integers n and h (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 n lines contains two integers xi1 and xi2 (1≤xi1<xi2≤109) — the endpoints of the i-th ascending air flow segment. No two segments intersect, and they are given in ascending order.

Output
Print one integer — the maximum distance along 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.

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

CodeForces 1041D Glider 枚举+二分相关推荐

  1. Codeforces Round #509 D - Glider(枚举二分前缀和)

    题意: 给出滑翔机的高度h 以及有n段上升气流区间,滑翔机在非上升气流区间会以每前进一个单位 高度就下降一个单位的 速度坠落 ,而在有上升气流的区间,滑翔机将持续前进,并不会下落,问在一维坐标上的哪个 ...

  2. 1041D - Glider(思维+二分+前缀和)

    https://codeforces.com/problemset/problem/1041/D 思路: 可以发现,选择的起点在当前某个线段区间的左端点一定不会更差. 于是On枚举起点位置,快速找到其 ...

  3. codeforces 1041D Glider

    题目链接:http://codeforces.com/problemset/problem/1041/D 题目简单说明:就是给定一个高度,然后有人从飞机跳下,通过滑翔机让自己每秒向右移动一个单位同时向 ...

  4. codeforces1041D Glider(二分/前缀和/贪心)

    题目链接:codeforces 1041D 题目思路: 显然,为了尽可能覆盖多的区间,起点一定是某段区间的左端点,故枚举左端点,二分查找终点,下降的高度用前缀和记录即可.具体参见代码. 参考代码: # ...

  5. 【HDU 5936 --- Difference】折半枚举+二分

    [HDU 5936 --- Difference]折半枚举+二分 Description Little Ruins is playing a number game, first he chooses ...

  6. 【CodeForces - 1041D】Glider (枚举起点,双指针 或 二分终点,思维)(知识点总结)

    题干: A plane is flying at a constant height of hh meters above the ground surface. Let's consider tha ...

  7. Glider CodeForces - 1041D

    http://codeforces.com/contest/1041/problem/D 先合并气流 得k个互不相交的气流 然后对(i-1)个空隙做前缀和 枚举以每个气流为起点的 可以飞到哪里 二分实 ...

  8. 【Codeforces 1041D】Glider

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 二分. 枚举每一个上升区的起始位置作为起点(这样做肯定是最优的),然后如果没有掉在地上的话就尽量往右二分(只有上升区之间的间隙会让他往下掉) ...

  9. Codeforces 685C Optimal Point (二分、不同类型距离的相互转换)

    题目链接 https://codeforces.com/contest/685/problem/C 题解 我怎么又还差最后一步的时候放弃了然后往别的方向上想了一小时才发现这个思路能做-- 首先二分答案 ...

最新文章

  1. Microbiome综述|植物内部微生物的相互作用
  2. centos6.7部署solr-6.3.0
  3. 各大视觉技术看透女神吗
  4. 《系统集成项目管理工程师》必背100个知识点-80项目变更管理在软件项目管理中的主要活动内容...
  5. 11.25个推TechDay X 中生代技术全国巡回沙龙北京站
  6. hive表定义(3种方式)
  7. 编程高手之路——闭包函数
  8. Mysql insert without auto-increase when duplicate
  9. BOS系统的设计与实现
  10. 云队友丨盘点,到底盘什么?
  11. 编译go版本的supervisord
  12. 【案例】星环科技×某能源企业:数据中台实践
  13. zookepper单机集群安装记录
  14. java中arSigal_基于AR模型谱估计算法(Yule-Walker方法与Burg方法)的C++实现
  15. js复制方法navigator.clipboard兼容性处理,控制台直接执行报错 DOMException: Document is not focused
  16. 北京理工大学-嵩天python语言程序设计-9-Python计算生态概览
  17. 网上千万不要在非官方直营店铺买的商品排行榜
  18. SDWAN与区块链——SDWAN CHEAP
  19. 红米note5解锁教程_红米Note 5A解锁BL教程_红米Note5A获取解锁码进行解锁
  20. 虚拟机里Win98装XP Luma主题

热门文章

  1. 开源示波器ADALM2000介绍、原理图学习
  2. Joomla模块位置教程
  3. Vue+Echarts监控大屏实例五:呼叫中心监控模板实例
  4. php报错:Notice: iconv(): Wrong charset, conversion from `GBK' to `UTF8' is not allowed
  5. Java入门:绘制简单图形
  6. 最新去水印网页版源码
  7. 免费的车辆违章车首页接口封装
  8. 三维物体计算全息及显示
  9. html页面散落樱花原理,jQuery实现Web页面樱花坠落的特效_亦心_前端开发者
  10. 灵遁者油画《岁月静好》:孤独是狂欢后的寂静