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;
    } 
  • 相关阅读:
    BestCoder Round #65
    Codeforces Round #334 (Div. 2)
    二叉搜索树(排序二叉树)
    二叉搜索树 POJ 2418 Hardwood Species
    差分约束系统 POJ 3169 Layout
    思维题(转换) HDU 4370 0 or 1
    SPFA+Dinic HDOJ 3416 Marriage Match IV
    图论 SRM 674 Div1 VampireTree 250
    SPFA(建图) HDOJ 4725 The Shortest Path in Nya Graph
    SPFA(负环) LightOJ 1074 Extended Traffic
  • 原文地址:https://www.cnblogs.com/ylrwj/p/10644539.html
Copyright © 2011-2022 走看看