码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces1138-A(D题)Sushi for Two

时间:2019-07-22 23:55:50      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:+=   ber   customer   present   size   idt   width   out   数字   

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

标签:+=   ber   customer   present   size   idt   width   out   数字   

原文地址:https://www.cnblogs.com/yuanhang110/p/11229248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!