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;
    } 
  • 相关阅读:
    2016华为实习编程题:停车管理系统
    lintcode:单词切分
    2016网易实习生编程题:数组中两个数的和等于sum
    2016网易实习生编程题:n个骰子的和等于m
    2016百度编程题:钓鱼比赛
    2016百度编程题:裁减网格纸
    2016百度编程题:罪犯转移
    JAVA面试基础
    扔硬币问题
    随机数生成随机数
  • 原文地址:https://www.cnblogs.com/ylrwj/p/10644539.html
Copyright © 2011-2022 走看看