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
题解:
我是先做到它的升级版:此题升级版
我大概讲一下吧:
不难发现:如果(l,r),那么(l+1,r)必然也合法。
所以我们枚举左端点,右端点是不断递增的,所以是线性的。
可以用ST表做,也可以用优先队列做。
优先队列代码:
#include<bits/stdc++.h>
using namespace std;
#define N 100050
int n,val[N],flag[N];
struct Node
{
int id,val;
bool operator <(const Node&b)const
{return val<b.val;}
};
template<typename T>void read(T&x)
{
int k=; char c=getchar();
x=;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit();
while(isdigit(c))x=x*+c-'',c=getchar();
x=k?-x:x;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
read(n);
for(int i=;i<=n;i++)read(val[i]);
int r=,ans=;
priority_queue<Node>mx,mi;
for(int i=;i<=n;i++)
{
while(r+<=n&&(mi.empty()||
(fabs(val[r+]-mx.top().val)<=&&fabs(val[r+]+mi.top().val)<=)))
{
mx.push(Node{r+,val[r+]});
mi.push(Node{r+,-val[r+]});
r++;
}
ans=max(ans,r-i+);
flag[i]=;
while(!mi.empty()&&flag[mi.top().id])mi.pop();
while(!mx.empty()&&flag[mx.top().id])mx.pop();
}
printf("%d\n",ans);
}
ST表代码
#include<bits/stdc++.h>
using namespace std;
#define N 100050
int n,val[N];
int mm[N],mi[N][],mx[N][];
template<typename T>void read(T&x)
{
int k=; char c=getchar();
x=;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit();
while(isdigit(c))x=x*+c-'',c=getchar();
x=k?-x:x;
}
void init_ST(int n)
{
mm[]=-;
for(int i=;i<=n;i++)
{
mm[i]=(i&(i-))==?mm[i-]+:mm[i-];
mx[i][]=mi[i][]=val[i];
}
for(int i=;i<=;i++)
for(int j=;j+(<<i)-<=n;j++)
{
mi[j][i]=min(mi[j][i-],mi[j+(<<(i-))][i-]);
mx[j][i]=max(mx[j][i-],mx[j+(<<(i-))][i-]);
}
}
int rmq(int x,int y)
{
int k=mm[y-x+];
int ans=max(mx[x][k],mx[y-(<<k)+][k]);
ans-=min(mi[x][k],mi[y-(<<k)+][k]);
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
read(n);
for(int i=;i<=n;i++)read(val[i]);
init_ST(n);
int r=,ans=;
for(int i=;i<=n;i++)
{
while(r+<=n&&rmq(i,r+)<=)r++;
ans=max(ans,r-i+);
}
printf("%d\n",ans);
}