1. 程式人生 > >【Codeforces Round 333 (Div 2)B】【貪心 多指標】Approximating a Constant Range 給定陣列 相鄰元素波動為1 求差值不超1的最長序連續子序列

【Codeforces Round 333 (Div 2)B】【貪心 多指標】Approximating a Constant Range 給定陣列 相鄰元素波動為1 求差值不超1的最長序連續子序列

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 ofndata pointsa1, ..., an. There aren't any big jumps between consecutive data points — for each1 ≤ i < n, it's guaranteed that|ai + 1 - ai| ≤ 1.

A range[l, r]of data points is said to bealmost constantif the difference between the largest and the smallest value in that range is at most1

. Formally, letMbe the maximum andmthe minimum value ofaiforl ≤ i ≤ r; the range[l, r]is almost constant ifM - m ≤ 1.

Find the length of the longest almost constant range.