题目链接:

题目

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. WPF视频教程系列笔记

    视频二:XAML基础 1.顶级元素 <Window></Window>,<Page></Page>,<Application></Ap ...

  2. lua技巧分享之保护执行

    我们在c#/c++里为了防止调用出现异常的时候程序可以正常的执行,经常使用try{}catch{}的结构, 那么,语言简单的lua是怎么做到的呢?答案就在pcall 先简单的介绍一下这个函数: --尝 ...

  3. eBay 开发流程

    1[记录]注册成为eBay开发者(eBay Developers Program)+创建Sanbox Key和Production Key http://www.crifan.com/register ...

  4. MVC5 属性路由

    属性路由(attribute routing)是最新被引进到MVC5中的,它和传统路由共同组成了mvc5中的两种主要路由. 1. 高质量的url应该满足以下几点 域名便于记忆和拼写 简短 便于输入 可 ...

  5. 何为BFC

    BFC 定义 BFC(Block formatting context)直译为"块级格式化上下⽂文".它是⼀一个独⽴立的渲染区域,只有Block-level box参 与, 它规定 ...

  6. CSS Sprite 雪碧图制作

    CSS Sprite 雪碧图,简单来说就是: 为了提高网页的性能,减少加载次数,将一些不会经常随网站内容变化的小图标,集中放在一张大图上,将该图应作为background-image 嵌入页面中,在需 ...

  7. (三)Qt语言国际化

    Vs 2010+ Qt5 实现语言国际化 创建一个工程,cpp代码如下: 1.创建工程 #include "languageinternationalized.h" #includ ...

  8. iOS Foundation框架 -2.常用集合类简单总结

    Foundation框架中常用的类有:NSString.NSArray.NSSet.NSDictionary 以及它们对应的子类 NSMutableString.NSMutableArray.NSMu ...

  9. WINDOWS下PHP 的pear DB的安装(本地环境:PHP5.4.15+Apache+mysql)

    因为需要安装phpunit,要先装pear,网上的教程大多数是以双击go-pear.bat开始,但是我安装的php文件夹里压根没有这个文件. 经过几次搜索之后终于找到了办法. 解决步骤如下: 1.下载 ...

  10. sqlServer2008 手工注入

    接着上一篇的<mysql手工注入> 参考:http://hi.baidu.com/ciqing_s/item/971bf994365130accc80e5ed http://hi.baid ...