B. Approximating a Constant Range

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.

Input

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).

Output

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

Sample test(s)

input

5

1 2 3 3 2

output

4

input

11

5 4 5 5 6 7 8 8 8 7 6

output

5

来自 <http://codeforces.com/contest/602/problem/B>

Codeforces Round #333 (Div. 2)

【题意】:

n个数,相邻数的差不超过1.

求最长的区间,使得极差不超过1.

【解题思路】:

对于X,包含X的合法区间需要考虑X-1 X+1 X+2 X-2的位置:

用数组P[i]记录下至此 i 出现的最大位置;

若X-1的最大位置大于X+1的,则考虑X+1和X-2的位置即可;

相反,只需要考虑X-1和X+2的位置。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define inf 0x3f3f3f3f
#define LL long long
#define maxn 110000
#define IN freopen("in.txt","r",stdin);
using namespace std; int n;
int p[maxn]; int main(int argc, char const *argv[])
{
//IN; while(scanf("%d",&n)!=EOF)
{
int ans = -;
memset(p, , sizeof(p)); for(int i=;i<=n;i++){
int x;scanf("%d",&x); if(p[x-]>p[x+]) ans = max(ans, i-max(p[x+],p[x-]));
else ans = max(ans, i-max(p[x+],p[x-])); p[x] = i;
} printf("%d\n", ans);
} return ;
}

Codeforces 602B Approximating a Constant Range(想法题)的更多相关文章

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

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

  2. CF 602B Approximating a Constant Range

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

  3. 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 ...

  4. 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 ...

  5. FZU 2016 summer train I. Approximating a Constant Range 单调队列

    题目链接: 题目 I. Approximating a Constant Range time limit per test:2 seconds memory limit per test:256 m ...

  6. cf602B Approximating a Constant Range

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

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

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

  8. 【CodeForces 602B】G - 一般水的题2-Approximating a Constant Range

    Description When Xellos was doing a practice course in university, he once had to measure the intens ...

  9. CodeForces 111B - Petya and Divisors 统计..想法题

    找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...

随机推荐

  1. POJ 3468 A Simple Problem with Integers(树状数组)

    题目链接:http://poj.org/problem?id=3468 题意:给出一个数列,两种操作:(1)将区间[L,R]的数字统一加上某个值:(2)查询区间[L,R]的数字之和. 思路:数列A,那 ...

  2. java中的toString方法

    对于我这种用惯了C++的人来说,突然见到有人写java程序的时候竟然将整数和String类型的变量使用+连接到一起,感到非常奇怪,追究了下原因. 原来所有的java对象都有toString()方法,而 ...

  3. gulp edm测试

    gulp工具的 gulp-mailgun 插件可以将你的html代码,通过mailgun服务器来发送,用于测试,用这个工具发送邮件最适合不过了. 首先我们需要引入gulp和gulp-mailgun模块 ...

  4. Awesome Javascript(中文翻译版)

    [导读]:GitHub 上有一个 Awesome – XXX 系列的资源整理.awesome-javascript 是 sorrycc 发起维护的 JS 资源列表,内容包括:包管理器.加载器.测试框架 ...

  5. 深入学习android之AlarmManager

    对应AlarmManage有一个AlarmManagerServie服务程 序,该服务程序才是正真提供闹铃服务的,它主要维护应用程序注册下来的各类闹铃并适时的设置即将触发的闹铃给闹铃设备(在系统中,l ...

  6. 概述什么是OSGi框架

    现 在越来越多的Java开发人员在谈论OSGi是有其道理的.在几年前上学的时候我进行了比较多的Eclipse插件开发,当时就亲身感觉到Eclipse 插件体系的灵活与强大,而该体系与OSGi也可谓一脉 ...

  7. 二维树状数组(水题) POJ1195

    前段时间遇到线段树过不了,树状数组却过了的题.(其实线段树过得了的) 回忆了下树状数组. 主要原理,还是二进制位数,每一项的和表示其为它的前((最后一位1及其后)的二进制数)和,可从二进制图来看.(用 ...

  8. 终极解决方案:windows10开机黑屏,死机

    windows10开机黑屏,死机一般情况都是由于双显卡中的独立显卡驱动造成的! 那么试着升级一下你的BIOS吧!一定要在官网下载你对应的BIOS驱动,然后双击安装,这个时候就别再动机子了,让他自己刷新 ...

  9. ZOJ 1455 Schedule Problem(差分约束系统)

    // 题目描述:一个项目被分成几个部分,每部分必须在连续的天数完成.也就是说,如果某部分需要3天才能完成,则必须花费连续的3天来完成它.对项目的这些部分工作中,有4种类型的约束:FAS, FAF, S ...

  10. COCOS2D-X学习笔记(一)-----Node类的学习

    Node类(在3.0版本以下叫CCNode):节点类. 本文记录以下几个方法的学习笔记: init()和onEnter()这俩个方法都是CCNode的方法.其区别如下: 1.其被调用的顺序是先init ...