题目链接:

题目

I. Approximating a Constant Range

time limit per test:2 seconds

memory limit per test:256 megabytes

问题描述

When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?

You're given a sequence of n data points a1, ..., an. There aren't any big jumps between consecutive data points — for each 1 ≤ i < n, it's guaranteed that |ai + 1 - ai| ≤ 1.

A range [l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be the maximum and m the minimum value of ai for l ≤ i ≤ r; the range [l, r] is almost constant if M - m ≤ 1.

Find the length of the longest almost constant range.

输入

The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of data points.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).

输出

Print a single number — the maximum length of an almost constant range of the given sequence.

样例

input

5

1 2 3 3 2

output

4

input

11

5 4 5 5 6 7 8 8 8 7 6

output

5

题意

求最大值最小值相差小于2的最大区间长度

题解

这一题由于相邻的数据最多差一,可以直接做,但是这里贴一个单调队列的模板吧,毕竟更通用,

开两个单调队列来维护窗口最大最小值。(这里的实现是有先队列+时间戳)下标其实就相当于时间戳了,对于窗口l,r,小于l的都是过期的。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define X first
#define Y second
#define mp make_pair
using namespace std; typedef __int64 LL;
const int maxn=1e5+10;
int arr[maxn];
priority_queue<pair<int,int> > mi,ma; int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%d",&arr[i]);
int l=0,r=0;
int ans=-1;
while(r<n){
mi.push(mp(-arr[r],r));
ma.push(mp(arr[r],r));
while(-mi.top().X<ma.top().X-1){
l++;
while(mi.top().Y<l) mi.pop();
while(ma.top().Y<l) ma.pop();
}
ans=max(ans,r-l+1);
r++;
}
printf("%d\n",ans);
return 0;
}

FZU 2016 summer train I. Approximating a Constant Range 单调队列的更多相关文章

  1. Codeforces 602B Approximating a Constant Range(想法题)

    B. Approximating a Constant Range When Xellos was doing a practice course in university, he once had ...

  2. Codeforces Round #333 (Div. 2) B. Approximating a Constant Range st 二分

    B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...

  3. cf602B Approximating a Constant Range

    B. Approximating a Constant Range time limit per test 2 seconds memory limit per test 256 megabytes ...

  4. Codeforces Round #333 (Div. 2) B. Approximating a Constant Range

    B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...

  5. codeforce -602B Approximating a Constant Range(暴力)

    B. Approximating a Constant Range time limit per test 2 seconds memory limit per test 256 megabytes ...

  6. 【32.22%】【codeforces 602B】Approximating a Constant Range

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【CodeForces 602C】H - Approximating a Constant Range(dijk)

    Description through n) and m bidirectional railways. There is also an absurdly simple road network — ...

  8. CF 602B Approximating a Constant Range

    (●'◡'●) #include<iostream> #include<cstdio> #include<cmath> #include<algorithm& ...

  9. #333 Div2 Problem B Approximating a Constant Range(尺取法)

    题目:http://codeforces.com/contest/602/problem/B 题意 :给出一个含有 n 个数的区间,要求找出一个最大的连续子区间使得这个子区间的最大值和最小值的差值不超 ...

随机推荐

  1. Hadoop YARN配置参数剖析—RM与NM相关参数

    注意,配置这些参数前,应充分理解这几个参数的含义,以防止误配给集群带来的隐患.另外,这些参数均需要在yarn-site.xml中配置. 1.    ResourceManager相关配置参数 (1) ...

  2. Directadmin清空所有Tickets命令

    利用一条命令就快速实现了清空所有Tickets的方法,希望此例子对大家有帮助.     即可清空所有工单,包括系统提示 :> /usr/local/directadmin/data/admin/ ...

  3. Github 访问时出现Permission denied (public key)

    一. 发现问题: 使用 git clone 命令时出现Permission denied (public key) . 二. 解决问题: 1.首先尝试重新添加以前生成的key,添加多次,仍然不起作用. ...

  4. 一次MVVM+ReactiveCocoa实践

    前言 学习MVVM和ReactiveCocoa(简称RAC)也有一段时间了,不过都仅限于看博客,一直对这两个东西很感兴趣,觉得很创新,也一直想找个机会在项目中实践一下,但是还是有一些顾虑,毕竟没有实践 ...

  5. Objective-C中的封装、继承、多态、分类

    封装的好处: 过滤不合理的值 屏蔽内部的赋值过程 让外界不必关注内部的细节 继承的好处: 不改变原来模型的基础上,拓充方法 建立了类与类之间的联系 抽取了公共代码 坏处:耦合性强(当去掉一个父类,子类 ...

  6. 20150515--关于IIS的备忘(WIN7)

    一.IIS服务位置: 1)控制面板--程序和功能 2)点击打开或关闭Windows功能, 3)Internet服务信息(英文:internet information services)--Web管理 ...

  7. python基础:自定义函数

    一.背景 在学习函数之前,一直遵循:面向过程编程,即:根据业务逻辑从上到下实现功能,其往往用一长段代码来实现指定功能,开发过程中最常见的操作就是粘贴复制,也就是将之前实现的代码块复制到现需功能处,如下 ...

  8. c/c++面试总结(1)

    最近在找新的工作,在找工作中遇到很多面试题,大多数让我很难堪,再次让我认识到自己的知识的匮乏,上份工作是以应届生的身份,所有当时进项目组也没有很多要求,进入项目组后自己还算好学(自己以为),之前也没有 ...

  9. 十个最常见的Java字符串问题

    翻译自:Top 10 questions of Java Strings 1.怎样比较字符串?用”==”还是用equals()? 简单地说,”==”测试两个字符串的引用是否相同,equals()测试两 ...

  10. MVC5_学习笔记_1_CodeFirst

    MVC5_EF6_1/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) *//* Author: Nicolas Hery - ...