题目描述

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的序列

考虑两个单调队列储存下标

每次用队列头计算答案

当两队列头差值不满足条件时,直接跳到两个最值下表最近的那个的位置

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ;
int n,k;
int a[maxn];
int q1[maxn],q2[maxn];
int main () {
scanf("%d%d",&k,&n);
for(int i=;i<=n;i++)
scanf("%d",a+i);
q1[]=q2[]=;int be=;
int h1=,t1=,h2=,t2=;
int ans=;
for(int i=;i<=n;++i) {
while(h1<=t1&&a[q1[t1]]<a[i])t1--;
while(h2<=t2&&a[q2[t2]]>a[i])t2--;
q1[++t1]=q2[++t2]=i;
while(a[q1[h1]]-a[q2[h2]]>k) {
if(q1[h1]<q2[h2]) be=q1[h1]+,h1++;
else be=q2[h2]+,h2++;
}
ans=max(ans,i-be+);
}
printf("%d\n",ans);
return ;
}

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

  1. P3512 [POI2010]PIL-Pilots

    P3512 [POI2010]PIL-Pilots我一开始打的O(n^2)(最坏情况)的算法.枚举区间长度.60分 #include<iostream> #include<cstdi ...

  2. P3512 [POI2010]PIL-Pilots-洛谷luogu

    刚研究完单调队列和单调栈 于是就找题做了 发现了这道蓝题 以为很简单 就着手来写了 然而 并不是我想的那样 只是有一点点思路 无奈 还是看了题解 好吧题解是真的挺好的 ---------------- ...

  3. Luogu P3496 [POI2010]GIL-Guilds(贪心+搜索)

    P3496 [POI2010]GIL-Guilds 题意 给一张无向图,要求你用黑(\(K\))白(\(S\))灰(\(N\))给点染色,且满足对于任意一个黑点,至少有一个白点和他相邻:对于任意一个白 ...

  4. [洛谷P3512 [POI2010]PIL-Pilots]

    题目链接: 传送门走这里 题目分析: 感觉不是很难啊--不像是蓝题(AC量也不像)恶意评分? 少打了一个+1调了半天,就这样居然还能过60pts?我思路和题解第一篇高度重合是什么鬼啊,太过分了吧本来还 ...

  5. BZOJ 2085 luogu P3502 [POI2010]Hamsters (KMP、Floyd、倍增)

    数组开小毁一生-- 题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2085 这题在洛谷上有个条件是"互不包含",其实 ...

  6. Luogu P3511 [POI2010]MOS-Bridges

    题目 二分答案然后混合图欧拉回路,因为没有SPJ所以就没写了,怕写了有锅.

  7. BZOJ 2096: [Poi2010]Pilots( set )

    用个set维护就可以水过去...O(NlogN) 应该可以用单调队列O(N).... --------------------------------------------------------- ...

  8. bzoj2096[Poi2010]Pilots 单调队列

    2096: [Poi2010]Pilots Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 983  Solved: 513[Submit][Statu ...

  9. 【BZOJ2096】[Poi2010]Pilots 双指针+单调队列

    [BZOJ2096][Poi2010]Pilots Description Tz又耍畸形了!!他要当飞行员,他拿到了一个飞行员测试难度序列,他设定了一个难度差的最大值,在序列中他想找到一个最长的子串, ...

随机推荐

  1. js采用正则表达式获取地址栏参数

    getQueryString:function(name) { var reg = new RegExp("(^|&)"+ name +"=([^&]*) ...

  2. cookies,sessionStorage和localStorage的相同点和不同点?

    相同点:都存储在客户端. 不同点: 1.存储大小: cookies数据大小不能超过4k sessionStorage和localStorage虽然也有存储大小的限制,但比cookies大得多,可以达到 ...

  3. JS函数节流和防抖

    看JS高级程序设计时,了解到一个概念--函数节流,是为了防止在高频率触发某些事件导致浏览器崩溃.最近又了解到另一个概念,防抖,感觉和函数节流很像,也查看了很多篇博文,算是理解了. 区别: 函数节流:频 ...

  4. 初涉k-d tree

    听说k-d tree是一个骗分的好东西?(但是复杂度差评??? 还听说绍一的kdt常数特别小? KDT是什么 KDT的全称是k-degree tree,顾名思义,这是一种处理多维空间的数据结构. 例如 ...

  5. [OpenJudge] 2727 仙岛寻药

    2727:仙岛求药 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进 ...

  6. 【OS_Linux】Linux系统中目录及文件管理

    1.Linux系统中目录的树状结构 目录 /bin 存放二进制可执行文件(ls,cat,mkdir等),常用命令一般都在这里. /etc 存放系统管理和配置文件 /home 存放所有用户文件的根目录, ...

  7. verdi知识点

    引用:http://blog.csdn.net/naclkcl9/article/details/5425936 1. verdi 加强了active anotation, active trace和 ...

  8. PHP发送邮件标题乱码的解决

    遇到问题:PHPMailer发送邮件时中文乱码,本来我的系统都是英文内容的,后来需求变化需要在标题中添加中文,但是在使用安卓自带邮件工具收取是出现乱码,而使用QQ邮箱查看确实正常的. 解决方法: 先用 ...

  9. Hi3519V101 Uboot和Kernel编译

    前面已经搭建好了Ubuntu下的海思开发环境,现在对编译Uboot和Kernel的过程做一个简单的记录.参考文档<Hi3519V101 U-boot 移植应用开发指南.pdf>和<H ...

  10. 基于链式链表的栈链式存储的C风格实现

    链式链表的头文件与CPP文件见前文 头文件: #ifndef _LINKSTACK_H_ #define _LINKSTACK_H_ typedef void LinkStack; //创建一个栈 L ...