CSU-暑假集训题 Sushi for Two

题目链接:http://codeforces.com/problemset/problem/1138/A

题目

Arkady invited Anna for a dinner to a sushi restaurant. The restaurant is a bit unusual: it offers n

pieces of sushi aligned in a row, and a customer has to choose a continuous subsegment of these sushi to buy.

The pieces of sushi are of two types: either with tuna or with eel. Let's denote the type of the i

-th from the left sushi as ti, where ti=1 means it is with tuna, and ti=2

means it is with eel.

Arkady does not like tuna, Anna does not like eel. Arkady wants to choose such a continuous subsegment of sushi that it has equal number of sushi of each type and each half of the subsegment has only sushi of one type. For example, subsegment [2,2,2,1,1,1]

is valid, but subsegment [1,2,1,2,1,2]

is not, because both halves contain both types of sushi.

Find the length of the longest continuous subsegment of sushi Arkady can buy.

Input

The first line contains a single integer n

(2n100000

) — the number of pieces of sushi.

The second line contains n

integers t1, t2, ..., tn (ti=1, denoting a sushi with tuna or ti=2

, denoting a sushi with eel), representing the types of sushi from left to right.

It is guaranteed that there is at least one piece of sushi of each type. Note that it means that there is at least one valid continuous segment.

Output

Print a single integer — the maximum length of a valid continuous segment.

Examples
Input
Copy
7
2 2 2 1 1 2 2
Output
Copy
4
Input
Copy
6
1 2 1 2 1 2
Output
Copy
2
Input
Copy
9
2 2 1 1 1 2 2 2 2
Output
Copy
6

Note

In the first example Arkady can choose the subsegment [2,2,1,1]

or the subsegment [1,1,2,2] with length 4

.

In the second example there is no way but to choose one of the subsegments [2,1]

or [1,2] with length 2

.

In the third example Arkady's best choice is the subsegment [1,1,1,2,2,2].

思路

将数值改变的下标位置记录在数组a中,然后遍历数组a,取相邻值的小值,最大的小值即为答案。

AC代码:

#include<iostream>
using namespace std;
int a[100010];
int ans=0;
int main(){
    int n;
    cin>>n;
    int pos=0;
    a[0]=0;
    int flag,num;
    cin>>flag;   
    for(int i=1;i<n;i++){
        cin>>num;
        if(flag!=num){
            flag=num;  // 
            a[++pos]=i;     
        }
    }
    a[++pos]=n; 
    for(int i=1;i<pos;i++){
        int t=min(a[i]-a[i-1],a[i+1]-a[i])*2;
        if(t>ans) ans=t;
    }
    cout<<ans<<endl;
    return 0; 
}

 

posted @ 2019-07-25 14:20  小小笼包包  Views(167)  Comments(0)    收藏  举报