加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Codeforces1138-A(D题)Sushi for Two

发布时间:2020-12-14 05:12:24 所属栏目:大数据 来源:网络整理
导读:Arkady invited Anna for a dinner to a sushi restaurant. The restaurant is a bit unusual: it offers? n 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

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,1,1]?is valid,but subsegment?[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,1]?or the subsegment?[1,1,2,2][1,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,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变量里。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读