#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 ...
随机推荐
- shell 中的$0 $1 $* $@ $# $$ $? $() $(())
$0: 脚本本身文件名称 : 命令行第一个参数,$2为第二个,以此类推 $*: 所有参数列表 $@: 所有参数列表 $#: 参数个数 $$: 脚本运行时的PID $?: 脚本退出码 ∗与@的区别 当命 ...
- 网卡bonding模式 - bond0、1、4配置
网卡bonding模式 - bond0.1.4配置 网卡bonding简介 网卡绑定就是把多张物理网卡通过软件虚拟成一个虚拟的网卡,配置完毕后,所有的物理网卡的ip和mac将会变成相同的.多网卡同时工 ...
- 关于Jmeter3.0,你必须要知道的5点变化
2016.5.18日,Apache 发布了jmeter 3.0版本,本人第一时间上去查看并下载使用了,然后群里或同事都会问有什么样变化呢?正好在网上看到一遍关于3.0的文章,但是是英文的.这里翻译一下 ...
- git如何回滚当前修改的内容?
git如何回滚当前修改的内容? 1.打开git gui,在工具栏上点击 commit ,选择 Revert Changes, 这里可以回滚单个文件: 2.一键回滚所有修改: 打开git gui,在工 ...
- Windows Mysql binlog 数据恢复
show variables like 'log_bin%'; 可以看到Mysql binlog为关闭状态,那我们去更改为开启状态
- Spark2 Dataset之collect_set与collect_list
collect_set去除重复元素:collect_list不去除重复元素select gender, concat_ws(',', collect_set(children)), ...
- OOA/D 01
建筑师一般不会为一栋100层的楼添加一个新的地下室,因为成本太高无疑会失败,但软件系统里提出类似改动需求时,他们通常都不会多想一下,相反他们会说:这只是一个简单的编程问题 可总会有一些看似极难完成.但 ...
- ubuntu14.04下编译支持opengl的opencv
在学习基于opencv的AR时,编译程序遇到报错,发现opencv不支持opengl.网上原因得知,在编译opencv时,opencv2.4以后的版本中默认ENABLE_OPENGL = NO,只需要 ...
- Python:正则表达式概念
#正则表达式内容非常多,网上的学习资源也是目不暇接,我从中筛选学习并且整理出以下 的学习笔记 一.正则表达式匹配过程: 1.依次拿出表达式和文本中的字符比较 2.如果每一个字符都能匹配,则匹配成功:一 ...
- ubuntu ssh 连接加速
vi /etc/ssh/sshd_config 最后加上 UseDNS no