#C++初学记录(算法考试1)
**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)的更多相关文章
- #C++初学记录(算法4)
A - Serval and Bus It is raining heavily. But this is the first day for Serval, who just became 3 ye ...
- #C++初学记录(贪心算法#结构体#贪心算法)
贪心算法#结构体 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋 ...
- #C++初学记录(算法效率与度量)
时间性能 算法复杂性函数: \[ f(n)=n^2 +1000n+\log_{10}n+1000 \] 当n的数据规模逐渐增大时,f(n)的增长趋势: 当n增大到一定值以后,计算公式中影响最大的就是n ...
- #C++初学记录(贪心算法#二分查找)
D - Aggressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 < ...
- #C++初学记录(算法3)
C - 不要62 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司 ...
- #C++初学记录(算法2)
A - Game 23 Polycarp plays "Game 23". Initially he has a number n and his goal is to trans ...
- #C++初学记录(算法测试2019/5/5)(深度搜索)
深度搜索:Oil Deposits GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块.他们通过专业设备,来分析每 ...
- #C++初学记录(sort函数)
sort函数 前言:当进行贪心算法的学习时,需要用到sort函数,因为初学c++汇编语言,sort的具体用法没有深入学习,所以这里进行sort学习记录并只有基础用法并借用贪心算法题目的代码. 百度百科 ...
- #C++初学记录(动态规划(dynamic programming)例题1 钞票)
浅入动态规划 dynamic programming is a method for solving a complex problem by breaking it down into a coll ...
随机推荐
- CF510B Fox And Two Dots(搜索图形环)
B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- iOS - ipa安装包大小优化
在App Store上显示的下载大小和实际下载下来的大小,我们通过下表做一个对比: iPhone型号 系统 AppStore 显示大小 下载到设备大小 iPhone6 10.2.1 91.5MB 88 ...
- mysql 存储过程用程序调的问题
记一下,存储过程加了commit结果用mysql客户端执行不报错,用jdbcTemplate执行就执行了一步,因为加了commit后面的程序都不跑了.另外 存储过程里面如果用java调的话select ...
- yii---load怎么使用
在用YII进行二次开发的时候,看到登录方法有一个load的方法: public function actionLogin() { if (Yii::$app->request->isPos ...
- R因子
factor(x = character(), levels, labels = levels, exclude = NA, ordered = is.ordered(x), nmax = NA) l ...
- 9.20 session
2018-9-20 13:14:40 Cookie session 参考 :http://www.cnblogs.com/liwenzhou/p/8343243.html 类视图 cbv 注册 ur ...
- PyCharm+Eclipse共用Anaconda的数据科学环境
1.安装anaconda2 安装好之后,本地python环境就采用anaconda自带的python2.7的环境. 2.安装py4j 在本地ctrl+r打开控制台后,直接使用pip安装py4j,因为a ...
- Python:容器、迭代对象、迭代器、生成器及yield关键字
在了解Python的数据结构时,容器(container).可迭代对象(iterable).迭代器(iterator).生成器(generator).列表/集合/字典推导式(list, ...
- web前端开发笔记(1)
一.HTML标签书写有哪些规范? 页面编码. 文档声明. 关键字与描述. 行内元素不能包含块级元素. a标签不能嵌套a标签. 标签名和属性必须用小写字母书写,属性必须加引号,标签必须闭合,单标签页必 ...
- tomcat与jetty的区别
转载自:https://www.cnblogs.com/fengli9998/p/7247559.html Jetty和Tomcat为目前全球范围内最著名的两款开源的webserver/servlet ...