B - Glider Gym - 101911B(二分)
standard output
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 (−109,h)(−109,h) to the point (109,h)(109,h) parallel with OxOx 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 OxOx 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 x1x1 and x2x2 (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 OxOx 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 .
The first line contains two integers nn and hh (1≤n≤2⋅105,1≤h≤109)(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 nn lines contains two integers xi1xi1 and xi2xi2 (1≤xi1<xi2≤109)(1≤xi1<xi2≤109) — the endpoints of the ii -th ascending air flow segment. No two segments intersect, and they are given in ascending order.
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.
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
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)=19999999991999999899−(−100)=1999999999 .
题意:给你n个无重力地带,飞机可以从高度h的任意位置开始滑行,问你最远能滑行多远。
思路:(orz做的时候想三分,结果wa了,经过网上题解的调教,原来二分就可以了)首先,我们肯定是从某个无重力地带开始滑行,但是挨个枚举肯定不行,然后就二分Orz
二分注重的就是个单调性,这题比较隐蔽,我们可以发现从左往右,价值越来越大(无重力地带的总长度是越来越长,免费长度,相当于白给),另外花费(两两无重力地带之间的区域,即飞机会下降的区域)也是随着右移越来越大,这样我们可以预处理价值和花费,注意cost【n】 = 2e9+1,因为最后一个无重力地带后面是无限长的。
这样,对于第一个起点,我们拥有高度h可以花费,也就是说找到第一个大于等于高度h的花费,可以用lowerbound;当然,但我们要想后枚举起点的时候h可以加上该起点之前的前缀和(毕竟终点要在起点之后,二分终点的时候加上起点前的值就相当于从该起点后查询)
#include<bits/stdc++.h>
using namespace std; int n,h;
const int maxn = 2e5+; int l[maxn];
int r[maxn]; int add[maxn];
int cost[maxn]; int main()
{
scanf("%d%d",&n,&h);
for(int i=;i<=n;i++)
{
scanf("%d%d",&l[i],&r[i]);
}
cost[n] = 2e9+;
for(int i=;i<n;i++)
{
cost[i] = cost[i-] + l[i+] - r[i];
add[i] = add[i-] + r[i] - l[i];
}
add[n] = add[n-] + r[n] - l[n];
int ans = ;
for(int i=;i<=n;i++)
{
int t = lower_bound(cost+,cost++n,cost[i-]+h)-cost;
ans = max(ans,add[t]-add[i-]+h);
}
printf("%d\n",ans);
}
B - Glider Gym - 101911B(二分)的更多相关文章
- Gym - 101911B Glider(前缀和+二分)
传送门:点我 A plane is flying at a constant height of hh meters above the ground surface. Let's consider ...
- E.Text Editor (Gym 101466E + 二分 + kmp)
题目链接:http://codeforces.com/gym/101466/problem/E 题目: 题意: 给你s串和t串,一个数k,求t的最长前缀串在s串中出现次数不少于k. 思路: 一眼二分+ ...
- Gym - 100989G 二分
链接:ECJTU 2018 Summer Training 1 - Virtual Judge https://vjudge.net/contest/236677#problem/G 谷歌翻译: 距 ...
- Gym - 101908G 二分答案+最大流
After the end of the truck drivers' strike, you and the rest of Nlogônia logistics specialists now h ...
- Hacker Cups and Balls Gym - 101234A 二分+线段树
题目:题目链接 题意:有编号从1到n的n个球和n个杯子. 每一个杯子里有一个球, 进行m次排序操作,每次操作给出l,r. 如果l<r,将[l,r]范围内的球按升序排序, 否则降序排, 问中间位置 ...
- Gym 102007I 二分 网络流
题意:给你一张图,每个城市有一些人,有不超过10个城市有避难所,避难所有容量上限,问最快多久可以让所有人进入避难所? 思路:二分时间,对于每个时间跑一遍最大流,判断最大流是不是人数即可.我们还需要用二 ...
- G - 土耳其冰淇凌 Gym - 101194D(二分答案 + 贪心检验)
熊猫先生非常喜欢冰淇淋,尤其是冰淇淋塔.一个冰淇淋塔由K个冰淇淋球堆叠成一个塔.为了使塔稳定,下面的冰淇淋球至少要有它上面的两倍大.换句话说,如果冰淇淋球从上到下的尺寸是A0, A1, A2,···, ...
- Gym - 100283F F. Bakkar In The Army —— 二分
题目链接:http://codeforces.com/gym/100283/problem/F F. Bakkar In The Army time limit per test 2 seconds ...
- Gym 101064 D Black Hills golden jewels (二分)
题目链接:http://codeforces.com/gym/101064/problem/D 问你两个数组合相加的第k大数是多少. 先sort数组,二分答案,然后判断其正确性(判断过程是枚举每个数然 ...
随机推荐
- Swift 新增fileprivate 详解
以前项目中只要用了private 那么在同一个文件同一个类中还是能访问的(比如一个类中写了一个extension) swift3.0现在不行了 新增了一个fileprivate 的访问控制 以前的p ...
- javaScript遍历对象、数组总结
javaScript遍历对象总结 1.使用Object.keys()遍历 返回一个数组,包括对象自身的(不含继承的)所有可枚举属性(不含Symbol属性). var obj = {'0':'a ...
- Android 基础 一 AndroidManifest.xml
一.概述 AndroidManifest.xml是Android应用的入口文件,它描述了package中暴露的组件(activities, services, 等等),他们各自的实现类,各种能被处理的 ...
- laravel 更新
public function update(Request $request, ResponseFactoryContract $response) { $user = $request->u ...
- laravel 里面结合关联查询 的when()用法
Laravel 5.6 里面的when用法: $name = $request->get('name'); //活动标题 $start_time = $request->get('star ...
- document.getElementsByClassName() 原生方法 通过className 选择DOM节点
<div id="box"> <div class="box">1</div> <div class="bo ...
- js 图片转换base64 base64转换为file对象
function getImgToBase64(url,callback){//将图片转换为Base64 var canvas = document.createElement('canvas'), ...
- 步步為營-98-MyAPI
1 通过NuGet程序管理包添加 Microsoft Asp.Net webAPI 2.2 的引用 2 添加两个文件夹Controllers和Models 2.1 在本地模拟数据库,所以在Model ...
- Django中模板使用
第一步:配置 1.在工程中创建模板目录templates. 2.在settings.py配置文件中修改TEMPLATES配置项的DIRS值:TEMPLATES = [ { 'BACKEND': 'dj ...
- hive sql常用整理-hive引擎设置
遇到个情况,跑hive级联insert数据报错,可以尝试换个hive计算引擎 hive遇到FAILED: Execution Error, return code 2 from org.apache. ...