Codeforces1138-A(D题)Sushi for Two

Posted yuanhang110

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces1138-A(D题)Sushi for Two相关的知识,希望对你有一定的参考价值。

Arkady invited Anna for a dinner to a sushi restaurant. The restaurant is a bit unusual: it offers nn 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 ii-th from the left sushi as titi, where ti=1ti=1 means it is with tuna, and ti=2ti=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][2,2,2,1,1,1] is valid, but subsegment [1,2,1,2,1,2][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 nn (2n1000002≤n≤100000) — the number of pieces of sushi.

The second line contains nn integers t1t1, t2t2, ..., tntn (ti=1ti=1, denoting a sushi with tuna or ti=2ti=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.

Note

 

In the first example Arkady can choose the subsegment [2,2,1,1][2,2,1,1] or the subsegment [1,1,2,2][1,1,2,2] with length 44.

 

In the second example there is no way but to choose one of the subsegments [2,1][2,1] or [1,2][1,2] with length 22.

 

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

题解

 1 #include<iostream>
 2 #include<math.h>
 3 using namespace std;
 4 int main() 
 5     int n,ans,max=1;
 6     cin>>n;
 7     int a[n],b[n-1];
 8     for(int i=0; i<n; i++) 
 9         cin>>a[i];
10     
11     for(int i=0; i<n-1; i++) 
12         b[i]=abs(a[i+1]-a[i]);
13     
14     for(int i=0; i<n-1; i++) 
15         if(b[i]==1) 
16             ans=1;
17             for(int j=1; j<=n/2; j++) 
18                 if((i-j)>=0&&(i+j)<n-1&&b[i-j]==0&&b[i+j]==0) 
19                     ans+=2;
20                     if(ans>=max) 
21                         max=ans;
22                     
23                  else 
24                     break;
25                 
26             
27         
28     
29     cout<<max+1;
30 

思路分析:题目是1和2两个数字代表两种寿司。要求选区连续的对称的如12或21或1122或2211或111222的字段并输出长度。

因为由1和2组成,它们的差就是0或1,如果是1122,它们的差就是010,遍历字符串找到所有为1的字符,再找它左边一个右边一个判断是否相等,再左边两个右边两个直到不相等,然后存取最大长度到max变量里。

 

以上是关于Codeforces1138-A(D题)Sushi for Two的主要内容,如果未能解决你的问题,请参考以下文章

D-Sushi for Two

Codeforces Round #797 (Div. 3) D, E, F, G题题解

CodeForces 10D. LCIS 最长公共上升子序列模板题 + 打印路径

Codeforces Round #464 (Div. 2) D题最小生成树

CodeForces 825B(模拟&贪心_D题)解题报告

Codeforces Round #624 (Div. 3) D 题