B. Light It Up
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Recently, you bought a brand new smart lamp with programming features. At first, you set up a schedule to the lamp. Every day it will turn power on at moment 00 and turn power off at moment MM. Moreover, the lamp allows you to set a program of switching its state (states are "lights on" and "lights off"). Unfortunately, some program is already installed into the lamp.
The lamp allows only good programs. Good program can be represented as a non-empty array aa, where 0<a1<a2<⋯<a|a|<M0<a1<a2<⋯<a|a|<M. All aiai must be integers. Of course, preinstalled program is a good program.
The lamp follows program aa in next manner: at moment 00 turns power and light on. Then at moment aiai the lamp flips its state to opposite (if it was lit, it turns off, and vice versa). The state of the lamp flips instantly: for example, if you turn the light off at moment 11 and then do nothing, the total time when the lamp is lit will be 11. Finally, at moment MM the lamp is turning its power off regardless of its state.
Since you are not among those people who read instructions, and you don't understand the language it's written in, you realize (after some testing) the only possible way to alter the preinstalled program. You can insert at most one element into the program aa, so it still should be a good program after alteration. Insertion can be done between any pair of consecutive elements of aa, or even at the begining or at the end of aa.
Find such a way to alter the program that the total time when the lamp is lit is maximum possible. Maybe you should leave program untouched. If the lamp is lit from xx till moment yy, then its lit for y−xy−x units of time. Segments of time when the lamp is lit are summed up.
Input
First line contains two space separated integers nn and MM (1≤n≤1051≤n≤105, 2≤M≤1092≤M≤109) — the length of program aa and the moment when power turns off.
Second line contains nn space separated integers a1,a2,…,ana1,a2,…,an (0<a1<a2<⋯<an<M0<a1<a2<⋯<an<M) — initially installed program aa.
Output
Print the only integer — maximum possible total time when the lamp is lit.
Examples
input
Copy
3 10 4 6 7
output
Copy
8
input
Copy
2 12 1 10
output
Copy
9
input
Copy
2 7 3 4
output
Copy
6
Note
In the first example, one of possible optimal solutions is to insert value x=3x=3 before a1a1, so program will be [3,4,6,7][3,4,6,7] and time of lamp being lit equals (3−0)+(6−4)+(10−7)=8(3−0)+(6−4)+(10−7)=8. Other possible solution is to insert x=5x=5 in appropriate place.
In the second example, there is only one optimal solution: to insert x=2x=2 between a1a1 and a2a2. Program will become [1,2,10][1,2,10], and answer will be (1−0)+(10−2)=9(1−0)+(10−2)=9.
In the third example, optimal answer is to leave program untouched, so answer will be (3−0)+(7−4)=6(3−0)+(7−4)=6.
在序列 a 中插入一个数 或者不插入 求 灯亮的最长时间。 插入一个数 之后 会改变之前 的灯 开 关 的 情况
l[i] 表示 前i小时 开灯的总时间, flag 来控制灯的开关。
l[n+1] - l[i] 表示 i 点之后的开灯总时长 -》 插入一个数之后 就变成了 关灯总时长了
m-a[i] 表示 a[i]-m 的总时长(包括开时长 关时长)
m-a[i]-(l[n+1] - l[i]-1) 就代表i点之后的总开灯时长
为了使开灯时长最长 在 i 附近 -1 最优(贪心思想)
l[i]+m-a[i]-(l[n+1] - l[i]-1) 就代表了 操作 之后 的总开灯市场。
神奇。
代码来自 https://blog.csdn.net/henu_xujiu/article/details/81484925
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define ll long long
#define N 1000005
int n,m,a[N],l[N],ans=0;
int main()
{
cin>>n>>m;
bool flag=1; //开关
l[0]=0;a[0]=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
l[i]=l[i-1]+flag*(a[i]-a[i-1]);
flag=!flag; //感觉超级巧妙
}
l[n+1]=l[n]+flag*(m-a[n]);//不加操作时的总时长
ans=l[n+1];
for(int i=1;i<=n;i++)
{
ans=max(ans,l[i]+m-a[i]-(l[n+1]-l[i])-1); //相通之后太巧妙了啊!!
/*m-a[i]是在这个点改变之后所有时长
l[n+1]-l[i] 原来总开灯时长-这点之前开灯时长=这点之后开灯时长。而改变这个点后。后面所有开灯时长变成了关灯时长
m-a[i]-(l[n+1]-l[i])也就是这个点 这个点改变之后所有时长- 关灯时长 =开灯时长
还要-1,因为 都是在点的临近点操作*/
}
cout<<ans <<endl;
}