2021-07-20 AcWing 3774. 亮灯时长
输入样例:
3 3 10 4 6 7 2 12 1 10 2 7 3 4
输出样例:
8 9 6
前缀和+贪心
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int a[N], s1[N], s2[N];
int main()
{
int T,n,m,res;
scanf("%d", &T);
while (T -- )
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
a[ ++ n] = m;
s1[n] = s2[n] = 0;
for (int i = n - 1; i >= 0; i -- )
{
s1[i] = s1[i+1], s2[i] = s2[i+1];
if (i%2 == 0) s1[i] += a[i+1]-a[i];
else s2[i] += a[i+1] - a[i];
}
res=s1[0];
for (int i = 0; i < n; i ++ )
{
int r = a[i+1]-a[i]-1;
if (!r) continue;
res = max(res, s1[0]-s1[i]+s2[i+1]+r);
}
printf("%d\n", res);
}
return 0;
}
本文来自博客园,作者:泥烟,CSDN同名, 转载请注明原文链接:https://www.cnblogs.com/Knight02/p/15799160.html