Maximal Continuous Rest

题目链接:http://codeforces.com/contest/1141/problem/B
B. Maximal Continuous Rest
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
5
1 0 1 0 1
outputCopy
2
inputCopy
6
0 1 0 1 1 0
outputCopy
2
inputCopy
7
1 0 1 1 1 0 1
outputCopy
3
inputCopy
3
0 0 0
outputCopy
0
Note
In the first example, the maximal rest starts in last hour and goes to the first hour of the next day.

In the second example, Polycarp has maximal rest from the 4-th to the 5-th hour.

In the third example, Polycarp has maximal rest from the 3-rd to the 5-th hour.

In the fourth example, Polycarp has no rest at all.

题目思路:就是给你个数组,看看里面有多少1,然后如果头和尾都是1还要再去判断,所以!我们就可以直接开一个二倍数组,把数字复制写进去一遍

例如:
输入:
5
1 0 1 0 1
我们可以把它变成1 0 1 0 1 1 0 1 0 1
这样就可以得到最长为2的了
5
1 1 1 0 1
变成
1 1 1 0 1 1 1 1 0 1
这样可以得到一个最长为4的
代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int a[400015];              //开一个二倍数组
int main(){
	int n,i;
	while(scanf("%d",&n)!=EOF){
		for(i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		int w=n;           //用w来进行数组的赋值存储
		for(i=0;i<n;i++){
			a[w]=a[i];
			w++;
		}
		int max_left=0;
		int max_right=0;
		int max=0;
		int left=0;
		int right=0;             //查找最多的1串
		while(right<=n*2){
			if(a[right]==1){
				right++;
			}
			else{
				if(right-left>max){
					max = right - left;
					max_left = left;
					max_right = right;
				}
				left = right;
				right++;
			}
		}
		printf("%d\n",max-1);//输出
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值