• 最长上升序列,首尾连接


    Each day in Berland consists of nn hours. Polycarp likes time management. That's why he has a fixed schedule for each day — it is a sequence a1,a2,,ana1,a2,…,an (each aiai is either 00 or 11 ), where ai=0ai=0 if Polycarp works during the ii -th hour of the day and ai=1ai=1 if Polycarp rests during the ii -th hour of the day.

    Days go one after another endlessly and Polycarp uses the same schedule for each day.

    What is the maximal number of continuous hours during which Polycarp rests? It is guaranteed that there is at least one working hour in a day.

    Input

    The first line contains nn (1n21051≤n≤2⋅105 ) — number of hours per day.

    The second line contains nn integer numbers a1,a2,,ana1,a2,…,an (0ai10≤ai≤1 ), where ai=0ai=0 if the ii -th hour in a day is working and ai=1ai=1 if the ii -th hour is resting. It is guaranteed that ai=0ai=0 for at least one ii .

    Output

    Print the maximal number of continuous hours during which Polycarp rests. Remember that you should consider that days go one after another endlessly and Polycarp uses the same schedule for each day.

    Examples

    Input
    5
    1 0 1 0 1
    
    Output
    2
    
    Input
    6
    0 1 0 1 1 0
    
    Output
    2
    
    Input
    7
    1 0 1 1 1 0 1
    
    Output
    3
    
    Input
    3
    0 0 0
    
    Output
    0
    


    题解:该题是考查最长上升子序列,但是要考虑首尾的连接,所以需要将首尾“连接”
    a[i+n]=a[i];
    代码如下:
    #include<iostream>
    #include<cstdio>
    using namespace std;
    const int maxn=2e5+10;
    int main()
    {
        int n;
        int cnt=0,le=0;
        int a[maxn];
        cin>>n;
        for(int i=0;i<n;i++)
       {
        cin>>a[i];
        a[i+n]=a[i];
       }
         
        for(int i=0;i<2*n;i++)
        {
            if(a[i]==1)
            {
               cnt++;
               le=max(le,cnt);
            }
            else
              cnt=0;
        }
        cout<<le<<endl;
        return 0;
    } 
  • 相关阅读:
    python Exception中的raise、assert
    python循环语句与其他编程语言不同之处
    【转】最大似然估计
    尝试发表代码
    奇异分解(SVD)
    Python学习笔记3-string
    梯度下降法
    前后端分离
    Trie树
    HashMap的工作原理以及代码实现,为什么要转换成红黑树?
  • 原文地址:https://www.cnblogs.com/ylrwj/p/10644539.html
Copyright © 2020-2023  润新知