【Codeforces Round 333 (Div 2)B】【贪心 多指针】Approximating a Constant Range 给定数组 相邻元素波动为1 求差值不超1的最长序连续子序列

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

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T> inline void gmax(T &a,T b){if(b>a)a=b;}
template <class T> inline void gmin(T &a,T b){if(b<a)a=b;}
const int N=1e5+10,M=0,Z=1e9+7,ms63=1061109567;
int n;
int a[N];
int main()
{
	while(~scanf("%d",&n))
	{
		for(int i=1;i<=n;++i)scanf("%d",&a[i]);a[0]=a[n+1]=-1;
		int ans=0;

		//i是第一个数的开头,j是第二个数的开头
		int i=1;
		int j=2;while(a[j]==a[i])++j;
		gmax(ans,j-i);
		
		/*然后,我们一直滚动,记录第一个数和第二个数的首位置,直到找到第三个数。并更新答案。
		之后再把第二个数当做第一个数,把第三个数当做的第二个数,并再重复上面的过程。直到最后。
		但是这里要注意,可能会出现{x x+1 x x+1 x+2}这种情况,
		所以我们可以向后转移的第三个数的位置需要动态更新,更新为最后一个a[j]!=a[j-1]的位置即可*/
		while(j<=n)
		{
			int x=a[j];int p=j;
			for(++j;a[j]==a[i]||a[j]==x;++j)if(a[j]!=a[j-1])p=j;
			gmax(ans,j-i);
			i=p;
		}
		printf("%d\n",ans);
	}
	return 0;
}
/*
【trick&&吐槽】
这题肯定有暴力水过的做法>_<
所以如果遇到实在不会的时候,可以考虑冲一发暴力哦!

【题意】
给你n(1e5)个数,每个数的范围都是在([1,1e5])。而且相邻两个数的差值只可能是{-1,0,+1}
我们接下来要找一个长度最长的序列,使得该序列的最大值与最小值之差最大只能为1。

【类型】
贪心 多指针

【分析】
如何才算是分析问题的合理思路?

最大值与最小值的差值最大只能为1,于是,对于这个长度最长的序列,其实就只存在两种情况——
情况1:所有数都相同,所有数都为x。
情况2:数值只可能为2个,x以及x+1。

首先,我们可以直接通过while(1)的方式,向右贪心滚动到不能滚动为止。
因为相邻的数的差值一定不超过1,于是这个时候,一定只存在2种可能——
(x x+1 x+2)
(x+1 x x-1)
我们只要记录第一个数,第二个数,第三个数的三个适当位置,并恰当转移,即可用O(n)复杂度时间解决问题。
然而一定要注意细节哦>_<!

【时间复杂度&&优化】
O(n)

*/


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值