刚研究完单调队列和单调栈

于是就找题做了

发现了这道蓝题

以为很简单

就着手来写了

然而

并不是我想的那样

只是有一点点思路

无奈

还是看了题解

好吧题解是真的挺好的

--------------------------------------------------------------

题目描述

In the Byteotian Training Centre, the pilots prepare for missions requiring extraordinary precision and control.

One measure of a pilot's capability is the duration he is able to fly along a desired route without deviating too much - simply put, whether he can fly steadily. This is not an easy task, as the simulator is so sensitive that it registers even a slightest move of the yoke1.

At each moment the simulator stores a single parameter describing the yoke's position.

Before each training session a certain tolerance level  is set.

The pilots' task then is to fly as long as they can in such a way that all the yoke's position measured during the flight differ by at most . In other words, a fragment of the flight starting at time  and ending at time  is within tolerance level  if the position measurements, starting with -th and ending with -th, form such a sequence  that for all elements  of this sequence, the inequality holds.

Your task is to write a program that, given a number  and the sequence of yoke's position measurements, determines the length of the longest fragment of the flight that is within the tolerance level .

给定n,k和一个长度为n的序列,求最长的最大值最小值相差不超过k的序列

输入输出格式

输入格式:

In the first line of the standard input two integers are given,  and  (), separated by a single space, denoting the tolerance level and the number of yoke's position measurements taken.

The second line gives those measurements, separated by single spaces. Each measurement is an integer from the interval from  to .

第一行两个有空格隔开的整数k(0<=k<=2000,000,000),n(1<=n<=3000,000),k代表设定的最大值,n代表序列的长度。第二行为n个由空格隔开的整数ai(1<=ai<=2000,000,000),表示序列。

输出格式:

Your program should print a single integer to the standard output:

the maximum length of a fragment of the flight that is within the given tolerance level.

一个整数代表最大的符合条件的序列

输入输出样例

输入样例#1: 复制

3 9
5 1 3 5 8 6 6 9 10
输出样例#1: 复制

4

说明

样例解释:5, 8, 6, 6 和8, 6, 6, 9都是满足条件长度为4的序列

---------------------------------------------------------------------------------------

n<=3000000,单调队列。

因为要同时维护最大值和最小值,所以需要两个单调队列,记录序号和最大最小值,然后

从左往右枚举一遍O(n)的复杂度即可。

单调队列的实现以最大值为例,当队列非空且队尾的数没有现在枚举到的这个数大时,就

让队尾的数出队,因为在接下来的情况中它们永远不会是一个区间的最大值。最小值的队列同理。把当前位置加入最大最小值队列。然后比较最大值队列的队首即最大值和最小值,如果它们的差超过了k,就看一下最大值和最小值的队首哪个的序号更小,让序号更小的出队,同时更新当前队列的最前节点的位置,继续比较两个的队首,直到最大最小值的差小于等于k时,用当前位置减去当前队列的最前节点的位置再加一,就是目前的符合条件的序列长度,与最长值比较即可。

当前队列的最前节点的位置初始值为1.

#include<bits/stdc++.h>
using namespace std; typedef long long ll;
ll hx = ,tx = ,hn = ,tn = ,len=,pre=; //最大值最小值的“头指针”“尾指针”
ll k,a[],qx[],qn[],n; //qx存最大值,qn存最小值 inline ll read()
{
ll ans = ,op = ;
char ch = getchar();
while(ch <'' && ch >'')
{
if(ch == '-')
op = -;
ch = getchar();
}
while(ch >= '' &&ch <= '')
{
(ans *= ) += (ch - '');
ch = getchar();
}
return ans * op;
} //快读 int main()
{
k = read();
n = read();
for(ll i=; i<=n; i++)
a[i] = read();
qx[]=;
qn[]=;
for(int i=;i<=n;i++)
{
while(hx <= tx && a[qx[tx]] < a[i])
tx--;
while(hn <= tn && a[qn[tn]] > a[i])
tn--;
qx[++tx] = i;
qn[++tn] = i;
while(a[qx[hx]] - a[qn[hn]] >k)
{
if(qx[hx] < qn[hn])
{
pre = qx[hx]+;
hx++;
}
else
{
pre = qn[hn]+;
hn++;
}
}
len=max(len,i-pre+);
}
printf("%lld",len);
return ;
}

P3512 [POI2010]PIL-Pilots-洛谷luogu的更多相关文章

  1. P1654 OSU!-洛谷luogu

    传送门 题目背景 原 <产品排序> 参见P2577 题目描述 osu 是一款群众喜闻乐见的休闲软件. 我们可以把osu的规则简化与改编成以下的样子: 一共有n次操作,每次操作只有成功与失败 ...

  2. 【原创】洛谷 LUOGU P3366 【模板】最小生成树

    P3366 [模板]最小生成树 题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入输出格式 输入格式: 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N ...

  3. 【原创】洛谷 LUOGU P3371 【模板】单源最短路径

    P3371 [模板]单源最短路径 题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数.出 ...

  4. 【原创】洛谷 LUOGU P3373 【模板】线段树2

    P3373 [模板]线段树 2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格式: 第 ...

  5. 【原创】洛谷 LUOGU P3372 【模板】线段树1

    P3372 [模板]线段树 1 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别 ...

  6. P1440 求m区间内的最小值--洛谷luogu

    题目描述 一个含有n项的数列(n<=2000000),求出每一项前的m个数到它这个区间内的最小值.若前面的数不足m项则从第1个数开始,若前面没有数则输出0. 输入输出格式 输入格式: 第一行两个 ...

  7. P2251 质量检测--洛谷luogu

    传送门 题目描述 为了检测生产流水线上总共N件产品的质量,我们首先给每一件产品打一个分数A表示其品质,然后统计前M件产品中质量最差的产品的分值Q[m] = min{A1, A2, ... Am},以及 ...

  8. P4550 收集邮票-洛谷luogu

    传送门 题目描述 有n种不同的邮票,皮皮想收集所有种类的邮票.唯一的收集方法是到同学凡凡那里购买,每次只能买一张,并且买到的邮票究竟是n种邮票中的哪一种是等概率的,概率均为1/n.但是由于凡凡也很喜欢 ...

  9. P3200 [HNOI2009]有趣的数列--洛谷luogu

    ---恢复内容开始--- 题目描述 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所有的奇数项满足a1<a3& ...

  10. 逆元-P3811 【模板】乘法逆元-洛谷luogu

    https://www.cnblogs.com/zjp-shadow/p/7773566.html -------------------------------------------------- ...

随机推荐

  1. 【Dubbo&&Zookeeper】6、 给dubbo接口添加白名单——dubbo Filter的使用

    在开发中,有时候需要限制访问的权限,白名单就是一种方法.对于Java Web应用,Spring的拦截器可以拦截Web接口的调用:而对于dubbo接口,Spring的拦截器就不管用了. dubbo提供了 ...

  2. blfs(systemv版本)学习笔记-制作一个简单的桌面系统

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 大概思路: lfs(系统)+xorg(驱动)+i3-wm(窗口+桌面)+lightdm(显示管理器+登录管理器) 链接: lfs ...

  3. 禁用 Gnome Shell 默认的 Ubuntu Dock 和 Ubuntu AppIndicators 扩展

    以前折腾的时候禁用过,现在已经忘记目录了,结果今天手贱把系统从 18.04 升级到了 18.10 ,很多东西都要重新搞过,而且用惯了 mac 已经不熟悉 linux 上瞎折腾的那一套了,简直坑爹.. ...

  4. 【代码笔记】Web-ionic-按钮

    一,效果图. 二,代码.index.html文件如下所示. <!DOCTYPE html> <html> <head> <meta charset=" ...

  5. 【读书笔记】iOS-深入解剖对等网络

    协议本身是一个运行在UDP之上的定制协议.我所以决定使用一个定制协议很简单.首先,当前这个任务看起来足够简单,因此与尝试改进一个现在协议相比,直接构建一个定制协议更为容易.其次,定制协议可以将开销减少 ...

  6. Angular 2基础(一) 环境搭建

    Angular2是一款开源JavaScript库,由Google维护,用来创建页面应用程序.正式发布于2016年9月,基于ES6开发. 一.准备工作 使用Angular2开发,需要预先做一些配置上的配 ...

  7. Angular2 富文本编辑器 ng2-ckeditor 的使用

    本文介绍如何在 Angular 中使用 ng2-ckeditor 控件,示例代码基于 angular 6.0.2,node 8.11.2,  ng2-ckeditor 4.9.2 环境   1. 安装 ...

  8. spring cloud 配置文件application.yml和bootstrap.yml 的定位,区别和联系总算是有一点明白了

    最近在启用springcloud配置中心server的东西,在整理属性资源的时候,突然发现:用了这么久的springboot,为什么会配置两个属性文件同时存在(application.yml/prop ...

  9. (后台)java 读取excel 文件 Unable to recognize OLE stream 错误

    原因:不支出读取 excel 2007 文件(*.xlsx).只支持 excel 2003 (*.xls). 光修改文件后缀不行,需要文件另存(或者导出)为 .xls Excel 1997-2004 ...

  10. [Demo_01] MapReduce 实现密码 Top10 统计

    0. 说明 通过 MapReduce 实现密码 Top10 统计 通过两次 MapReduce 实现 1. 流程图 2. 程序编写 密码 Top10 统计代码