B. Approximating a Constant Range
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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
Note

In the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.

In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].

题意:求最大区间长度  区间要求满足:区间最大值与最小值的差小于等于1

题解:

例如

5

1 2 3 3 2

差值分别为 2-1=1;

3-2=1;

3-3=0;

2-3=-1;  另外  it's guaranteed that |ai + 1 - ai| ≤ 1.

可以判断 当连续的差值或相隔差值为0 的两个差值 相等时 该段区间结束 更新最大值

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,a;
int main()
{
scanf("%d",&n);
scanf("%d",&a);
int cha=0;
int judge=0;
int l=0,r=0;
int ans=0,exm;
for(int i=1; i<n; i++)
{
scanf("%d",&exm);
cha=exm-a;//计算差值
a=exm;
if(cha==0)//差值为零 相等时
continue;
if(cha!=judge)//当前差值与 之前一个差值比较
{
judge=cha;//更新到当前区间
r=i;
}
else
{
if(i-l>ans)//更新区间大小
ans=i-l;
l=r;
r=i;
}
}
if(n-l>ans)//特列 后端 都相等
ans=n-l;
cout<<ans<<endl;
return 0;
}

  

Codeforces Round #333 (Div. 2) B的更多相关文章

  1. Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp

    C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  2. Codeforces Round #333 (Div. 1) B. Lipshitz Sequence 倍增 二分

    B. Lipshitz Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/601/ ...

  3. Codeforces Round #333 (Div. 2) C. The Two Routes flyod

    C. The Two Routes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/602/pro ...

  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. Codeforces Round #333 (Div. 2) A. Two Bases 水题

    A. Two Bases Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/602/problem/ ...

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

  7. Codeforces Round #333 (Div. 1) D. Acyclic Organic Compounds trie树合并

    D. Acyclic Organic Compounds   You are given a tree T with n vertices (numbered 1 through n) and a l ...

  8. Codeforces Round #333 (Div. 2)

    水 A - Two Bases 水题,但是pow的精度不高,应该是转换成long long精度丢失了干脆直接double就可以了.被hack掉了.用long long能存的下 #include < ...

  9. Codeforces Round #333 (Div. 1)--B. Lipshitz Sequence 单调栈

    题意:n个点, 坐标已知,其中横坐标为为1~n. 求区间[l, r] 的所有子区间内斜率最大值的和. 首先要知道,[l, r]区间内最大的斜率必然是相邻的两个点构成的. 然后问题就变成了求区间[l, ...

随机推荐

  1. 【setUp-tearDown】线程组开始,结束各执行一次

    使用setUp线程组的方式  ——> 开始 使用tearDown线程组 的方式 ——>结束

  2. 手机端网页返回顶部js代码

    <!DOCTYPE html>  <html>  <head>  <meta http-equiv="Content-Type" cont ...

  3. 正则表达式 和 re 模块

    正则表达式究竟是什么? 在一些网站注册的时候需要输入手机号码,当你输入一个错误的手机号码的时候,会提示你输入的手机号码格式错误 那么他究竟是如何判断的呢? 我们用Python代码进行表示: phone ...

  4. Java VisualVM使用

    Java VisualVM Java VisualVM官网 Java VisualVM介绍 Java VisualVM is a tool that provides a visual interfa ...

  5. LeetCode 141——环形链表

    1. 题目 2. 解答 2.1 方法 1 定义快慢两个指针,慢指针每次前进一步,快指针每次前进两步,若链表有环,则快慢指针一定会相遇. /** * Definition for singly-link ...

  6. 解决python中文编码错误问题

    对于初学者而言,编码问题或许还没有没重视起来,但是编码问题是中文开发者必须面对的.今天来看下python开发中如何解决编码问题.注意:本篇讲的是最常见的一种编码问题,其他编码问题,如json函数引起的 ...

  7. 深入理解Java对象序列化(转载)

    原文地址:http://developer.51cto.com/art/201202/317181.htm 1. 什么是Java对象序列化 Java平台允许我们在内存中创建可复用的Java对象,但一般 ...

  8. lintcode-42-最大子数组 II

    42-最大子数组 II 给定一个整数数组,找出两个 不重叠 子数组使得它们的和最大. 每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 注意事项 子数组最少包含一个数 样例 给出数组 [1 ...

  9. OSG学习:使用已有回调示例

    回调的类型有很多种,一般很容易就想到的是UpdateCallBack,或者EventCallBack,回调的意思就是说,你可以规定在某件事情发生时启动一个函数,这个函数可能做一些事情.这个函数就叫做回 ...

  10. Perfmon - 脚本自动监控

    PerfMon-Windows性能监视器是个好东西,可以辅助我们分析发生问题时间段服务器资源占用情况,但是部署性能计数器确实一个相当麻烦的事情,往往这种枯燥的事别人还做不了,只能由我们这些希望获取到P ...