Gym - 101911B 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 (−10^9,h)(−10^9,h) to the point (10^9,h)(10^9,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 x1 and x2 x2 (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 Ox 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⋅10^5,1≤h≤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 nn lines contains two integers xi1xi1 and xi2xi2 (1≤xi1<xi2≤10^9)(1≤xi1<xi2≤10^9) — 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
3 4
2 5
7 9
10 11
10
5 10
5 7
11 12
16 20
25 26
30 33
18
1 1000000000
1 1000000000
1999999999 NOTE
In the first example if the glider can jump out at (2,4), then the landing point is (12,0), so the distance is 12−2=10
In the second example the glider can fly from (16,10) to (34,0), and the distance is 34−16=18
In the third example the glider can fly from (−100,1000000000) to (1999999899,0), so the distance is 1999999899−(−100)=1999999999
大概题意:
给出的是n个气流带(按顺序给出),和你现在所在的高度m。在气流带中不会下降,即维持进入气流带的高度。不在气流带则会每次下降一个单位。
下面n行是气流带的起点和终点。注意如果高度为0,恰好到下一个气流带的起点,也不能经过下一个加速带。
询问的是,起点的x值可以任你选择,最后降落下来到地面的点,离你选择起点的最远距离是多少。
因为是可以任意选起点,所以会有一个最大值。题目插图画的就是样例1的示例。
思路:看到题目给2秒,大暴力二分找✔。算了下复杂度是O(NlogN),n是2e5,也可以接受。
首先,要明确的一点是,起点肯定是某个加速带的起点,因为这样才能保证有更多的空间,留给后面经过更多的加速带(简单的贪心)
其次,既然是二分,那么肯定要是有序的,在这题我二分的是从我选定的起点,最远到能到的终点,中间可以经过多少加速带。因为距离是递增的,所以可以二分。
即,把每个加速带中间的间隔,抽取出来作为一个数组,对这个数组进行二分,然后同时维护一个本身加速带的长度的一个前缀和,用来快速计算,经过加速带的距离。
最后答案肯定是 :能经过的加速带的距离+m 这个值的最大值
不知道还有没有更好的办法呀,还想学习一下。
暂时先这样子吧。
最后是代码:
#include<stdio.h>
#include<string.h>
#include<string>
#include<math.h>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
struct note{
int x,y;
}p[];
int a[];
int main()
{
vector<int>v;
int sum = ,cnt = ,n,m;
a[cnt++] = ;//a数组维护加速带的前缀和
scanf("%d %d",&n,&m);
for(int i = ; i < n ; i ++){
scanf("%d %d",&p[i].x,&p[i].y);
a[cnt] = a[cnt-] + p[i].y - p[i].x;
cnt++;
if(i > ){
sum += p[i].x-p[i-].y;
v.push_back(sum);
}//v向量用来保存加速带的前缀和
}
int ans = ;
for(int i = ;i <= v.size(); i++){
int pos = lower_bound(v.begin(),v.end(),(i==?:v[i-])+m)-v.begin();
sum = a[pos+]-a[i] + m;//加速带的长度+m ,即要更新的答案
ans = max(sum,ans);
}
printf("%d\n",ans);
}
/*
3 4
2 5
7 9
10 11 5 10
5 7
11 12
16 20
25 26
30 33 1 1000000000
1 1000000000
*/
Gym - 101911B Glider(前缀和+二分)的更多相关文章
- Glider(前缀和+二分)
题目链接:Glider Gym-101911B 解题分析:下落的高度一定,是h.在没有气流的地方每秒下落1:所以可以转化为经过无气流地带的时间总长为h. 那么很显然从一个有气流地带的开始,选择下落,那 ...
- Acwing:102. 最佳牛围栏(前缀和 + 二分)
农夫约翰的农场由 NN 块田地组成,每块地里都有一定数量的牛,其数量不会少于1头,也不会超过2000头. 约翰希望用围栏将一部分连续的田地围起来,并使得围起来的区域内每块地包含的牛的数量的平均值达到最 ...
- Codeforces 975 前缀和二分算存活人数 思维离直线速度相同判平行
A /* Huyyt */ #include <bits/stdc++.h> using namespace std; typedef long long ll; ]; ]; map< ...
- Codeforces 948 数论推导 融雪前缀和二分check 01字典树带删除
A. 全部空的放狗 B. 先O(NLOGNLOGN)处理出一个合数质因数中最大的质数是多少 因为p1 x1 x2的关系是 x2是p在x1之上的最小倍数 所以x1的范围是[x2-p+1,x2-1]要使最 ...
- B - Glider Gym - 101911B(二分)
output standard output A plane is flying at a constant height of hh meters above the ground surface. ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- Educational Codeforces Round 11 C. Hard Process 前缀和+二分
题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...
- D. Frets On Fire 前缀和+二分
这个题真的难了我一天了,这种方法一开始没想出来,后来看了题解后明白了大致思路开始自己做但是!!!但是自己实现的时候老是一些细节出错!!!,调bug调了得有一个小时,蠢死了,这道题我一定要好好总结,总结 ...
- P1314 聪明的质监员(前缀和+二分)
P1314 聪明的质监员 显然可以二分参数W 统计Y用下前缀和即可. #include<iostream> #include<cstdio> #include<cstri ...
随机推荐
- webpack环境搭建
环境搭建参考:Webpack+vue+element逐步搭建开发环境 webpack入门:http://www.jianshu.com/p/42e11515c10f ----------------- ...
- python_03 各种运算符
1.算数运算 2.比较运算 3.赋值运算 4.逻辑运算 先计算括号中表达式 计算顺序:and,or,not在一个表达式中从前到后计算, 若and前一个元素为false则立刻返回为False,不计算后面 ...
- jsfl 选择图层 选择帧 转化成mc
//打开fla var _openDOC = fl.openDocument("file:///E|TE/爱.fla"); //获取图层4的总帧 var _Length=fl.ge ...
- centos7 操作记录
centos7 firewall 命令查看已经开放的端口firewall-cmd --list-ports查看开放的服务firewall-cmd --list-services开启端口firewall ...
- poi excel
使用apache的poi包可以对excel进行操作读取和写入. 因excel分为xls的2003版和xlsx的2007版,poi在创建workbook时使用不同的类创建,因此需要注意区分xls. Wo ...
- vc/vs开发的应用程序添加dump崩溃日志<转>
原贴地址:https://blog.csdn.net/wangkui1331/article/details/78029940 vc/vs开发的应用程序出现崩溃的时候,由于没有任何记录,导致开发人员很 ...
- tips___代码规范
函数变量尽可能置于最小作用域内,并在变量声明时进行初始化 变量声明的位置最好离第一次使用的位置越近越好:应使用初始化的方式代替声明再赋值. int x=0; rather than int x; x ...
- numpy.distutils.system_info.NotFoundError: no lapack/blas resources found问题解决
操作环境 Python3.6 + Windows7 问题现象 利用pip自动安装seaborn/numpy/scipy(pip install seaborn)模块失败,提示numpy.distu ...
- 今天学习到的几条shell技巧
1.获取某个进程的进程号 PID=`ps aux | grep 进程名 | grep -v "grep" | awk '{print $2}'` 2.获取某个进程的CPU(同理可获 ...
- 【转】WinDbg调试器:启动程序时自动连接调试器方法
当我们调试服务进程或子进程时,无法直接用调试加载进程的方式启动,此时需要在启动程序时自动连接调试器方法: 第一步:注册表展开到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft ...