【小李冲鸭】CodeForces - 1141B 高端的题材往往需要最简洁的代码

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

中文版题目:

给你一个长度为n(n<=2e5)的循环数列,保证数列中至少含有一个0,数列中的每一项都只可能为0或1,求该数列中最长的连续1的数列是多长?

Input

第一行输入数列的项数n; 第二行输入数列中每一项的值。

Output

输出该循环数列中最长的连续1的长度。

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

Note

假设数列的第i项为ai; 第一个样例中{a5,a1}组成最大长度为2的连续1序列; 第二个样例中{a4,a5}组成最大长度为2的连续1序列; 第三个样例中{a3,a4,a5}组成最大长度为3的连续1序列; 第四个样例中数列中没有为1项,则连续1序列长度为0;

解题思路:就是找长度最大的连续的1序列,忌讳简单题复杂做!!!

(像我这样暴力的人往往死的很惨,因为你不可能考虑到所有特殊情况)

正确代码:(选自聚聚 简洁能ac)

#include <iostream>
#include <algorithm>
#include <cstdio>
 
using namespace std;
const int N = 4e5 + 10;
int n;
int a[N];
int res, len;
int main(){
	cin >> n;
	for (int i = 0; i < n; i ++){
		cin >> a[i];
		a[i + n] = a[i];
	}
	for (int i = 0; i < n * 2; i ++)
		if (a[i] == 1){
			len ++;
			res = max(res, len);
		}
		else len = 0;
	cout << res << endl;
	return 0;
}

这个代码有一个巧妙之处:就是他把a[]存了两遍,这样就可以轻松的计算出头一天晚上最后一小时,和这一天第一个小时那种连续的情况,大佬的代码,妙啊~

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值