题目链接:Glider Gym-101911B

解题分析:下落的高度一定,是h。在没有气流的地方每秒下落1;所以可以转化为经过无气流地带的时间总长为h。

     那么很显然从一个有气流地带的开始,选择下落,那么问题来了,一个一个去试然后一个一个计算他的路径去维护一个最大值吗?未免太过麻烦,所给数据有

     那么大。

解题关键二分+前缀和

二分查找用:lower_bound()

      int x=lower_bound(a, a+n, val)-a;

      其中i返回值为数组元素值大于等于val的第一个下标。相应地,如果数组中的所有元素的值都小于val,则返回值为数组最后一个元素下标的下一个下标

具体见代码注释:

/* 所给的数据:气流范围是按顺序给的,方便直接求前缀和*/
# include <iostream>
# include <stdio.h>
# include <string.h>
# include <string>
# include <iomanip>
# include <algorithm>
# include <ctime>
# include <cmath>
# include <climits>
# include <cstdlib>
# include <utility>
# include <bitset>
# include <cctype>
# include <cassert>
# include <set>
# include <map>
# include <deque>
# include <queue>
# include <stack>
# include <vector>
# include <functional>
using namespace std;
 
typedef long long ll;
const int maxn=2e5+;
const ll mod=1e9+;
const int eps=1e-;
const double pi=acos(-1.0);
# define mem(a,x) memset((a),(x),sizeof((a)))
# define gcd(a,b) (__gcd(a, b))
# define lcm(a,b) (a*b/__gcd(a, b))
# define lson l,m,rt<<
# define rson m+,r,rt<<|
# define lowbit(x)(x&(-x))
 
struct nod
{
int l, r;
}x[maxn];
 
int a[maxn], b[maxn];//a[i]表示前i个气流带的总长度,b[i]表示前i个无气流带(人的下降区)的总长度。
int main()
{
int n, h;
cin>>n>>h;
for(int i=; i<=n; i++ )
cin>>x[i].l>>x[i].r;
int maxx=-;
a[]=x[].r-x[].l;
b[]=;
for(int i=; i<=n; i++ )
{
a[i] = a[i-] + x[i].r - x[i].l;
b[i] = b[i-] + x[i].l - x[i-].r;
}
b[n+]=0x3f3f3f3f;
 
for(int i=; i<=n; i++ )
{
int pos=lower_bound(b+, b++n, b[i]+h)-b;//利用二分查找第一个大于等于b[i]+h的点的位置pos
maxx = max(maxx, a[pos-]-a[i-]);//a[pos-1]-a[i-1]为经过的当我们下降h时经过的上升气流的长度
}
cout<<maxx+h<<endl;
return ;
}

Glider(前缀和+二分)的更多相关文章

  1. Gym - 101911B Glider(前缀和+二分)

    传送门:点我 A plane is flying at a constant height of hh meters above the ground surface. Let's consider ...

  2. Acwing:102. 最佳牛围栏(前缀和 + 二分)

    农夫约翰的农场由 NN 块田地组成,每块地里都有一定数量的牛,其数量不会少于1头,也不会超过2000头. 约翰希望用围栏将一部分连续的田地围起来,并使得围起来的区域内每块地包含的牛的数量的平均值达到最 ...

  3. Codeforces 975 前缀和二分算存活人数 思维离直线速度相同判平行

    A /* Huyyt */ #include <bits/stdc++.h> using namespace std; typedef long long ll; ]; ]; map< ...

  4. Codeforces 948 数论推导 融雪前缀和二分check 01字典树带删除

    A. 全部空的放狗 B. 先O(NLOGNLOGN)处理出一个合数质因数中最大的质数是多少 因为p1 x1 x2的关系是 x2是p在x1之上的最小倍数 所以x1的范围是[x2-p+1,x2-1]要使最 ...

  5. Educational Codeforces Round 11 C. Hard Process 前缀和+二分

    题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...

  6. D. Frets On Fire 前缀和+二分

    这个题真的难了我一天了,这种方法一开始没想出来,后来看了题解后明白了大致思路开始自己做但是!!!但是自己实现的时候老是一些细节出错!!!,调bug调了得有一个小时,蠢死了,这道题我一定要好好总结,总结 ...

  7. P1314 聪明的质监员(前缀和+二分)

    P1314 聪明的质监员 显然可以二分参数W 统计Y用下前缀和即可. #include<iostream> #include<cstdio> #include<cstri ...

  8. CF978C Letters【前缀和+二分查找/几房几号】

    [链接]:CF978C [分析]:在前缀和数组种二分找到>=询问数的位置,根据位置就好操作了 [代码]: #include<bits/stdc++.h> using namespac ...

  9. cf1119d Frets On Fire 前缀和+二分

    题目:http://codeforces.com/problemset/problem/1119/D 题意:给一个数n,给出n个数组的第一个数(a[0]=m,a[1]=m+1,a[2]=m+2,... ...

随机推荐

  1. 过滤器 & 监听器 & 拦截器

    过滤器: https://blog.csdn.net/MissEel/article/details/79351231 https://blog.csdn.net/qq_32363305/articl ...

  2. [golang]使用gomail发邮件(在Go中发送电子邮件的最佳方式)

    1 前言 定义邮箱服务器连接信息,如果是网易邮箱 pass填密码,qq邮箱填授权码(客户端专用密码). gomail包: go get gopkg.in/gomail.v2 更多功能可以参考 http ...

  3. virsh 添加虚拟交换机

    virsh 添加虚拟交换机 来源 https://blog.csdn.net/a1987463004/article/details/90905981 vim /etc/libvirt/qemu/ne ...

  4. Kubernetes Storage Persistent Volumes

    链接:https://kubernetes.io/docs/concepts/storage/persistent-volumes/ 支持的参数,比如mountOptions在这里可以找到 删除正在被 ...

  5. 2019 咪咕文化java面试笔试题 (含面试题解析)

    本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.咪咕文化等公司offer,岗位是Java后端开发,最终选择去了咪咕文化. 面试了很多家公司,感觉大部分公司考察的点 ...

  6. 常用模块 - configparse模块

    一.简介 configparser模块在Python中是用来读取配置文件的,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节点(section),每个节可以有多个参数(键=值 ...

  7. 《区块链DAPP开发入门、代码实现、场景应用》笔记5——区块链福利彩票的设计

    笔者一直强调,一定要利用区块链的特点来解决行业存在的问题,并且该问题最好用区块链解决或者说只能用区块链解决.彩票行业就是个例子. 在讲解代码之前,首先讲解一下业务设计,如图6.15所示. 图6.15 ...

  8. Android为TV端助力之点击Textview无效

    记录一下如果有两个Textview都有点击事件,那么不能给Textview同时设置 android:focusable="true"android:focusableInTouch ...

  9. LeetCode刷题:Reverse Words in a String(翻转字符串中的单词)

    题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...

  10. webdriver切换frame的方法

    iframe: iframe 就是一个特殊的html 元素, 它在原来的html 范围内,开辟了一个新的HTML. iframe 元素会创建包含另外一个文档的内联框架(即行内框架) 理解:网页嵌套网页 ...