zoukankan      html  css  js  c++  java
  • 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变量里。

  • 相关阅读:
    面试汇总——说一下CSS盒模型
    各厂面试题汇总
    为网页背景添加一个跟随鼠标变幻的动态线条
    lnmp一键安装包
    java独立小程序实现AES加密和解密
    git命令note
    不可思议的纯 CSS 滚动进度条效果
    Git-Book
    CentOS搭建Git服务器及权限管理
    vim编辑器里shift + 3 出现高亮问题,怎么取消掉
  • 原文地址:https://www.cnblogs.com/yuanhang110/p/11229248.html
Copyright © 2011-2022 走看看