Codeforces —— 思维(第3周)

Codeforces 1138A  Sushi for Two

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 ??=1ti=1means it is with tuna, and ??=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 ?n (2≤?≤1000002≤n≤100000) — the number of pieces of sushi.

The second line contains ?n integers ?1t1, ?2t2, ..., ??tn (??=1ti=1, denoting a sushi with tuna or ??=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.

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][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 2 组成的字符串,找连续的1和2,这个由1组成的串和2组成的串必须也是连续的,还得保证两个字串的数目相同,然后输出这个有1和2组成的子串的最大长度

eg:

2 2 1 1 1 2 2 2 2

可以是 [ 2 2 1 1]   、[ 2 1 ] 、[ 1 1 1 2 2 2] 、[1 2] 

其中最大的长度就是6

1 2 1 2 1 2

可以是 [ 1 2] 、[ 2 1 ]....

 

思路:

这个思路也是WA一次,T一次想到的,不过还是有点麻烦


找一段连续的1或者2,把找到的个数存到数组里

数组里交替存1或者2的数目

然后相邻的进行比较长度,两个相邻的要求出一个较小的,才能保证数目相同,然后对每一次比较的结果比较求出一个最大的

记:比较相邻的时候结果要记得 ×2 ,因为一个是1或者2的长度,而结果是1 2一起的长度

 

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
typedef long long LL;
using namespace std;
#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f

int a[100005];
int b[50005];
int main()
{
    int n,cnt=0;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];

    int p=0,q=0;

    for(int i=0;i<n;i++){

        if(a[i]==2){ // 遇到2,如果前边已经出现了1,则把1的数目存到数组里,并初始化然后进行下一次计数
            q++;
            if(p!=0){
                b[cnt++]=p;
                p=0;
            }
        }

        if(a[i]==1){
            p++;
            if(q!=0){
                b[cnt++]=q;
                q=0;
            }
        }
    }

    if(q!=0)
        b[cnt++]=q;
    if(p!=0)
        b[cnt++]=p;

   /*
    对样例 2 2 1 1 1 2 2 2 2 来说
   数组里存的是 2 3 4  
   */


    int ans=-1;
    for(int i=1;i<cnt;i++){
        int k=min(b[i],b[i-1]);
        k*=2;
        ans=max(ans,k);
    }

    cout<<ans<<endl;

 

 


第二种:改的WA的思路

2 2 1 1 1 2 2 2 1 1

思路是这样的:

先记录下1和2的个数,然后两个一比较,例如1 2 或者2 1,如果遇到1或者2

就是这样的情况 1 2 1   或者  2 1 2 (原谅我嘴拙不知道怎么解释)

然后在计数第三个数之前,就进行比较一次(长度比较同方法一)

然后将与第三个数相同的数计数变量初始化

例如1 2 1 用p计数1,q计数2

遇到第三个数1的时候,将p初始化,重新计数1的数目,然后比较2 1 的长度


CODE:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
typedef long long LL;
using namespace std;
#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f

int a[100005];
int b[50005];
int c[50005];

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];

    int p=0,q=0;
    int cnt=0,cnt1=0;
    int flag=0;
    int ans=-1;

    for(int i=0;i<n;i++)
    {
        if(i!=0&&a[i]!=a[i-1])
            flag++;

         if(flag==2)
         {
            int k=min(p,q);
            k*=2;
            ans=max(ans,k);
            flag=1;

            if(a[i]==2)
                q=0;
            if(a[i]==1)
                p=0;
        }

        if(a[i]==1)
            p++;
        if(a[i]==2)
            q++;
    }

    int k=min(p,q);
    k*=2;
    ans=max(ans,k);
    cout<<ans<<endl;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值