**B - Maximal Continuous Rest **

Each day in Berland consists of n hours. Polycarp likes time management. That's why he has a fixed schedule for each day — it is a sequence a1,a2,…,an (each ai is either 0 or 1), where ai=0 if Polycarp works during the i-th hour of the day and ai=1 if Polycarp rests during the i-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 n (1≤n≤2⋅105) — number of hours per day.

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

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

正确代码

#include<bits/stdc++.h>
using namespace std;
const int MM = 2e5+5;
int a[MM];
int main()
{
int n, res = 0, maxx = 0;
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
if(a[i] == 1)
{
res++;
if(i == n && maxx < res)
{
//if(maxx < res)
//{
maxx = res;
res = 0;
// }
}
}
else
{
if(maxx < res)
{
maxx = res;
}
res = 0;
}
}
int res1 = 0, res2 = 0;
for(int i = 1; i <= n; i++)
{
if(a[i] == 1) res1++;
else break;
}
for(int i = n; i >= 1; i--)
{
if(a[i] == 1) res2++;
else break;
}
int tt = 0;
if(res1 != n)
tt = res1+res2;
//cout << maxx << endl;
printf("%d\n", tt > maxx? tt:maxx);
return 0;
}

代码理解

该题不难,主要目的是判断1的连续个数,主要难点是如何判断开头和结尾1连续的个数,如果想到在两边同时进行判断则可以很迅速的编写完成代码,首先用if语句判断1的连续性,即用数组a进行储存1和0,若a[i]1则进行下一个循环,若a[i]!=1即a[i]0则跳出循环,与此同时进行两边同时判断1的连续性函数,即从开始a[0]判断1连续的个数,若a[i]==0则结束循环进行从后向前判断a[n-1]是否等于零,判断出前后连续1的个数则进行相加即代码中的res1+res2,最后用最大值max和res1+res2比较大小,最后输出最大的那一项。

#C++初学记录(算法考试1)的更多相关文章

  1. #C++初学记录(算法4)

    A - Serval and Bus It is raining heavily. But this is the first day for Serval, who just became 3 ye ...

  2. #C++初学记录(贪心算法#结构体#贪心算法)

    贪心算法#结构体 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋 ...

  3. #C++初学记录(算法效率与度量)

    时间性能 算法复杂性函数: \[ f(n)=n^2 +1000n+\log_{10}n+1000 \] 当n的数据规模逐渐增大时,f(n)的增长趋势: 当n增大到一定值以后,计算公式中影响最大的就是n ...

  4. #C++初学记录(贪心算法#二分查找)

    D - Aggressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 < ...

  5. #C++初学记录(算法3)

    C - 不要62 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司 ...

  6. #C++初学记录(算法2)

    A - Game 23 Polycarp plays "Game 23". Initially he has a number n and his goal is to trans ...

  7. #C++初学记录(算法测试2019/5/5)(深度搜索)

    深度搜索:Oil Deposits GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块.他们通过专业设备,来分析每 ...

  8. #C++初学记录(sort函数)

    sort函数 前言:当进行贪心算法的学习时,需要用到sort函数,因为初学c++汇编语言,sort的具体用法没有深入学习,所以这里进行sort学习记录并只有基础用法并借用贪心算法题目的代码. 百度百科 ...

  9. #C++初学记录(动态规划(dynamic programming)例题1 钞票)

    浅入动态规划 dynamic programming is a method for solving a complex problem by breaking it down into a coll ...

随机推荐

  1. slf4j + log4j 是如何初始化的

    SLF4J的全称是 Simple Logging Facade for Java(简单java日志门面) SLF4J自己不提供具体的日志功能实现,只是提供了一个统一的日志门面,在这个统一的门面之下,用 ...

  2. python中的null值

    在一个没有接口文档的自动化测试中,只能通过抓包及查日志查看发送的信息,其中有一个接口发送的信息如下: enable_snapshot": true, "new_size" ...

  3. Unity3D笔记十九 持久化数据

    1.PlayerPrefs类(生命周期???) 1.1 保存与读取数据 在C#中类似缓存.Cookie.Session等保存数据的,但是有点区别的是在C#中如果在取值时没有取到默认值则返回值是NULL ...

  4. REM+SVG Sprite,web app案例

    REM+SVG Sprite,构建新时代web app <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN&quo ...

  5. iOS - 开源框架、项目和学习资料汇总(UI篇)

    上下拉刷新控件 1. MJRefresh --仅需一行代码就可以为UITableView或者CollectionView加上下拉刷新或者上拉刷新功能.可以自定义上下拉刷新的文字说明.(推荐)2. SV ...

  6. java使用AES256解密

    网上关于java用AES加密解密的文章有很多,我这里只用到解密(加密是服务器那边做^_^),所以更简洁一些: public class AES256Utils { private static fin ...

  7. 记我的第一个python爬虫

    捣鼓了两天,终于完成了一个小小的爬虫代码.现在才发现,曾经以为那么厉害的爬虫,在自己手里实现的时候,也不过如此.但是心里还是很高兴的. 其实一开始我是看的慕课上面的爬虫教学视屏,对着视屏的代码一行行的 ...

  8. CVPR2018资源汇总

    CVPR 2018大会将于2018年6月18~22日于美国犹他州的盐湖城(Salt Lake City)举办. CVPR2018论文集下载:http://openaccess.thecvf.com/m ...

  9. 推荐系统之最小二乘法ALS的Spark实现

    1.ALS算法流程: 初始化数据集和Spark环境----> 切分测试机和检验集------> 训练ALS模型------------> 验证结果-----------------& ...

  10. 区块链,Ethereum-Wallet

    https://blockchain.info/charts/transactions-per-second https://slock.it/     https://en.wikipedia.or ...