题目描述

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 ;
}

最新文章

  1. oracle--第一天议--bai
  2. hduoj 1455 &amp;&amp; uva 243 E - Sticks
  3. github上写blog
  4. centos mysql 操作
  5. iOS面向编码|iOSVideoToolbox:读写解码回调函数CVImageBufferRef的YUV图像
  6. cf E. George and Cards
  7. 改进了UI的界面
  8. apache: eclipse的tomcatPluginV插件下载
  9. windows下利用nginx 做IIS负载均衡
  10. 翻译--Thinking in React
  11. 验证码识别之w3cschool字符图片验证码(easy级别)
  12. Spring 使用介绍(九)—— 零配置(二)
  13. StringRedisTemplate常用API
  14. 爬取猫眼电影TOP100
  15. xml json
  16. Arduino的光敏传感器和超声波测距传感器测试代码
  17. 2018 Java线程热门面试题,你知道多少?
  18. Supervisor安装与配置(非守护进程管理工具)
  19. django需要了解的
  20. block、inode、superblock详解

热门文章

  1. 什么是二维数组?二维遍历?Java二维数组制作图片迷宫 使用如鹏游戏引擎制作窗口界面 附带压缩包下载,解压后双击start.bat启动
  2. 计算机应用 office系列 常用术语英文
  3. windows下使用gcc完成头文件和目标文件编译
  4. Mysql 5.7在Linux上部署及远程访问
  5. Centos7.2 上部署 FastDFS_V5.05
  6. python 中变量和对象
  7. PAT Basic 1070
  8. PAT Basic 1040
  9. struts 乱码
  10. tensorflow在各种环境下搭建与对比