AcWing 3810. 最长连续休息时间
原题链接
简单
作者:
NgAgo
,
2021-08-20 19:17:05
,
所有人可见
,
阅读 250
输入n
个数,用双指针循环2n
边即可
时间复杂度$O(n)$
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2 * 1e6 + 10;
int a[N];
int main()
{
int T;
cin >> T;
while (T -- )
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
int res = 0;
for (int i = 0, j; i < 2 * n; i ++ )
{
if(a[i] == 1)
{
j = i;
while (a[j % n] == 1) j ++ ;
res = max(res, j - i);
i = j -- ;
}
}
printf("%d", res);
puts("");
}
return 0;
}