【Virtual Judge】G - 一般水的题2-Approximating a Constant Range

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

Input

5
1 2 3 3 2

Output

4

Input

11
5 4 5 5 6 7 8 8 8 7 6

Output

5

Hint

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

读入数据时如果当前的和之前的相同则记录,处理时维护两个数字,题意的一个序列里最多两个不同数字,如果不符合就跳出,符合则判断下一个(j=b[j])

#include<stdio.h>
#include<algorithm>
using namespace std;

long long n,ans,len,cont,num1,num2=100005,a[100005],b[100005];
int main()
{
    scanf("%lld",&n);
    int i,j;
    for(i=1; i<=n; i++)
    {
        scanf("%lld",&a[i]);
        if(a[i]==a[i-1])
        {
            if(cont==0) //cont=0前一个不重复
                cont=b[i-1];
            b[i]=cont;
        }
        else
            b[i]=i-1;
        cont=0;
    }
    for(i=n; i>0; i--)
    {
        num1=a[i];
        num2=100005;
        len=1;
        j=i-1;
        while(j>0)
        {
            if(num1==a[j]||num2==a[j])
            {
                len+=j-b[j];
                j=b[j];
                continue;
            }
            else if(num2==100005)
            {
                num2=a[j];
                len+=j-b[j];
                j=b[j];
            }
            else break;
        }
        ans=max(ans,len);
        if(i<ans)break;
    }
    printf("%lld\n",ans);
    return 0;
}

  

时间: 2024-08-02 22:31:53

【Virtual Judge】G - 一般水的题2-Approximating a Constant Range的相关文章

【Virtual Judge】H - 大概做不来的题 Approximating a Constant Range

Description In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. There is also an absurdly simple road network — for each pair of different towns x and y, there is a bidirectional road between towns x and yif and onl

Codeforces 602B Approximating a Constant Range(想法题)

B. Approximating a Constant Range 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 suffi

专题一、简单搜索 - Virtual Judge

很久以前刷完了Virtual Judge上的简单搜索专题,现总结如下: POJ 1321 由于题目的数据范围比较小,可以直接dfs暴力.读入时记录每个空位的位置,保存在pX[]以及pY[]数组中.暴力的时候统计当前处理第几个空格以及当前处理到了第几行即可. #include <iostream> #include <memory.h> using namespace std; const int MAX = 128; long long ans; int N, K, nCnt; b

Virtual Judge使用指南

https://cn.vjudge.net/ Virtual Judge并不是常规的Online Judge平台,他通过爬取其他OJ的题目,让我们可以直接在VJ上查找并提交各种OJ的题目,然后将我们的题目通过他的账号(比如你在cf上会看到用户名vj1.vj2--在HDU上会看到张翼德.马孟起--)在真正的OJ上提交并把结果反馈给我们.大概就相当于一个平台一个中介吧.貌似ACM选手使用得较为普遍? 右上角注册登录: 界面这些OJ是他可以爬到题目的,也就是我们可以直接在VJ里做题的.每个图标也可以直

Ubuntu 14.04 Virtual Judge 的搭建

Ubuntu 14.04 Virtual Judge 的搭建 博客的排版可能不是很好,可以下载本文的PDF: Ubuntu 14.04Virtual Judge 的搭建 本人是一名ACM弱菜,由于集训队需要, 我们决定架设自己的OnlineJudge和Virtual Judge,于是开始了噩梦般的搭建. OJ的搭建,非常顺利,参考的是 Ubuntu Server 12.04搭建hustoj 这篇博客 写得非常好,其他不多说,如果有需要搭建自己学校的OJ的朋友,可以参考, 主要就是架设LAMP,其

G 最水的一道

G - Here Be Dragons The Triwizard Tournament's third task is to negotiate a corridor of many segments, and reach the other end. The corridor is N segments long. The ith segment is either empty or has a dragon. Harry cannot pass the dragon and will ha

HDU-1037-Keep on Truckin&#39;(HDU最水的题没有之一,为了练英语就来吧)

Keep on Truckin' Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10324    Accepted Submission(s): 7147 Problem Description Boudreaux and Thibodeaux are on the road again . . . "Boudreaux, we ha

【Virtual Judge】E - 特别水的题5-Wilbur and Swimming Pool

Description After making bad dives into swimming pools, Wilbur wants to build a swimming pool in the shape of a rectangle in his backyard. He has set up coordinate axes, and he wants the sides of the rectangle to be parallel to them. Of course, the a

hdu 5038 (2014北京网络赛G 排序水题)

题意:有n个数字,带入10000 - (100 - ai) ^ 2公式得到n个数,输出n个数中频率最大的数,如果有并列就按值从小到大都输出输出,如果频率相同的数字是全部的n个数,就输出Bad....题解:统计数字个数和频率,排序后输出. Sample Input36100 100 100 99 98 1016100 100 100 99 99 1016100 100 98 99 99 97 Sample OutputCase #1:10000Case #2:Bad MushroomCase #3