B. Approximating a Constant Range

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/602/problem/B

Description

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 Input

5
1 2 3 3 2

Sample Output

4

HINT

题意

给你n个数,要求你找到最长的区间,使得这个区间的最大值减去最小值之差的绝对值小于等于1

题解:

枚举每一个数,以这个数为这个区间的最小值,能够往左边延伸多少,往右边延伸多少

再枚举每一个数,以这个数为区间的最大值,能够往左边延伸多少,往右边延伸多少就好了

可以O(n) 也可以 像我一样 用倍增然后二分去找

代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define maxn 100005
int n;
int dp[maxn][];
int dp1[maxn][];
int a[maxn];
int L[maxn],R[maxn];
int mm[maxn];
void initrmp(int n)
{
mm[]=-;
for(int i=;i<=n;i++)
{
mm[i]=((i&(i-))==)?mm[i-]+:mm[i-];
dp[i][]=a[i];
dp1[i][]=a[i];
}
for(int j = ;j<=mm[n];j++)
for(int i=;i+(<<j)-<=n;i++)
dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
for(int j = ;j<=mm[n];j++)
for(int i=;i+(<<j)-<=n;i++)
dp1[i][j]=min(dp1[i][j-],dp1[i+(<<(j-))][j-]);
}
int queryMax(int l,int r)
{
int k = mm[r-l+];
return max(dp[l][k],dp[r-(<<k)+][k]);
}
int queryMin(int l,int r)
{
int k = mm[r-l+];
return min(dp1[l][k],dp1[r-(<<k)+][k]);
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
int Ans = ;
initrmp(n);
for(int i=;i<=n;i++)
{
int l=,r=i;
while(l<=r)
{
int mid = (l+r)/;
if(abs(a[i]-queryMin(mid,i))<= && abs(a[i]-queryMax(mid,i))<=)r=mid-;
else l=mid+;
}
L[i]=l;
l=i,r=n;
while(l<=r)
{
int mid = (l+r)/;
if(abs(a[i]-queryMin(i,mid))<= && abs(a[i]-queryMax(i,mid))<=)l=mid+;
else r=mid-;
}
R[i]=l-;
}
for(int i=;i<=n;i++)
Ans = max(Ans,R[i]-L[i]+);
for(int i=;i<=n;i++)
{
int l=,r=i;
while(l<=r)
{
int mid = (l+r)/;
if(abs(a[i]-queryMin(mid,i))<= && abs(a[i]-queryMax(mid,i))<=)r=mid-;
else l=mid+;
}
L[i]=l;
l=i,r=n;
while(l<=r)
{
int mid = (l+r)/;
if(abs(a[i]-queryMin(i,mid))<= && abs(a[i]-queryMax(i,mid))<=)l=mid+;
else r=mid-;
}
R[i]=l-;
}
for(int i=;i<=n;i++)
Ans = max(Ans,R[i]-L[i]+);
cout<<Ans<<endl;
}

Codeforces Round #333 (Div. 2) B. Approximating a Constant Range st 二分的更多相关文章

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

  2. Codeforces Round #333 (Div. 2)

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

  3. Codeforces Round #333 (Div. 2) B

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

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

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

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

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

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

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

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

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

随机推荐

  1. “FormCRUD.csProj.FormMain.Name”隐藏了继承的成员“System.Windows.Forms.Control.Name”。如果是有意隐藏,请使用关键字 new。

    一旦运行就显示:“FormCRUD.csProj.FormMain.Name”隐藏了继承的成员“System.Windows.Forms.Control.Name”.如果是有意隐藏,请使用关键字 ne ...

  2. 翻译【ElasticSearch Server】第一章:开始使用ElasticSearch集群(3)

    运行ElasticSearch(Running ElasticSearch) 让我们运行我们的第一个实例.转到bin目录并从命令行运行以下命令: ./elasticsearch –f (Linux o ...

  3. 动态执行linq 语句 NLinq

    using Evaluant.NLinq.Memory;using System.Collections.Generic;using Evaluant.NLinq;using System.Colle ...

  4. memcpy、memmove、memset及strcpy函数实现和理解

    memcpy.memmove.memset及strcpy函数实现和理解 关于memcpy memcpy是C和C++ 中的内存拷贝函数,在C中所需的头文件是#include<string.h> ...

  5. PHP:产生不重复随机数的方法

    来源:http://www.ido321.com/1217.html 无论是Web应用,还是WAP或者移动应用,随机数都有其用武之地.在最近接触的几个小项目中,我也经常需要和随机数或者随机数组打交道, ...

  6. 关于c3p0配置详细说明

    <!-- c3p0连接池配置 --> <property name="driverClass" value="${c3p0.driverClass}&q ...

  7. HDU 1707

    思路:标记课程表上的课程,询问时遍历课程表,再以字典序输出名字. #include<iostream> #include<stdio.h> #include<stdlib ...

  8. 内核源码分析之软中断(基于3.16-rc4)

    1.和软中断相关的数据结构: softing_vec数组(kernel/softirq.c) static struct softirq_action softirq_vec[NR_SOFTIRQS] ...

  9. hadoop多次格式化后,导致datanode启动不了,怎么办?(伪分布式)

    根据当初 hadoop 安装目录下 conf 目录的 core-site.xml 的设置,找到该目录:   进入该目录 在 data 和 name 文件夹下均有  current 文件夹 ,和 cur ...

  10. Ubuntu 安装 Sun JDK

    1. 下载    Oracle网站下载JDK7     http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1 ...