zoukankan      html  css  js  c++  java
  • 最长上升序列,首尾连接

    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中时间日期格式化符号的含义
    关于long long int和__int64用%I64d和%lld输出在不同编译语言下的正确性
    并查集 poj2236
    数据结构 最长公共子序列问题
    449B
    oj判题WA/RE怎么办
    如何准确区别python中strftime strptime的用法
    通过HttpWebRequest在后台对WebService进行调用 【转】
    利用maven 下依赖包
    Maven的安装
  • 原文地址:https://www.cnblogs.com/ylrwj/p/10644539.html
Copyright © 2011-2022 走看看