CF1000B.Light It Up
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 0 and turn power off at moment M. 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 a, where 0<a1<a2<⋯<a∣a∣<M. All ai must be integers. Of course, preinstalled program is a good program.
The lamp follows program a in next manner: at moment 0 turns power and light on. Then at moment ai 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 1 and then do nothing, the total time when the lamp is lit will be 1. Finally, at moment M 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 a, so it still should be a good program after alteration. Insertion can be done between any pair of consecutive elements of a, or even at the begining or at the end of a.
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 x till moment y, then its lit for y−x units of time. Segments of time when the lamp is lit are summed up.
最近,你购买了一盏全新的智能灯,它具备编程功能。起初,你为这盏灯设置了一个时间表:每天它都会在时刻 0 开启电源,并在时刻 M 关闭电源。此外,该灯允许你设置一个控制其状态(“开灯”与“关灯”)切换的程序。不幸的是,灯中已预装了一个程序。
该灯仅允许“好程序”。一个好程序可表示为一个非空数组 a,满足 0<a1<a2<⋯<a∣a∣<M,其中所有 ai 均为整数。当然,预装程序本身就是一个好程序。
灯按如下方式执行程序 a:在时刻 0,灯开启电源并点亮;随后在每个时刻 ai,灯立即翻转其状态(若之前亮着,则熄灭;反之亦然)。状态翻转是瞬时完成的:例如,若你在时刻 1 熄灭灯光且之后不再操作,则灯点亮的总时间为 1;最后,在时刻 M,灯无论当前状态如何,均强制关闭电源。
由于你并非那种会阅读说明书的人,且又看不懂说明书所用的语言,经过一番测试后,你意识到唯一可能修改预装程序的方式:你最多可在程序 a 中插入一个元素,使得修改后的程序仍为一个好程序。插入位置可以是 a 中任意两个相邻元素之间,也可以在 a 的开头或末尾。
请找出一种修改方案(包括不作任何修改),使得灯点亮的总时间达到最大。若灯从时刻 x 一直点亮至时刻 y,则其点亮时长为 y−x;所有点亮时间段的长度之和即为总点亮时间。
输入格式
First line contains two space separated integers n and M (1≤n≤105, 2≤M≤109) — the length of program a and the moment when power turns off.
Second line contains n space separated integers a1,a2,…,an (0<a1<a2<⋯<an<M) — initially installed program a.
第一行包含两个用空格分隔的整数 n 和 M(1≤n≤105,2≤M≤109)——分别表示程序 a 的长度以及断电时刻。
第二行包含 n 个用空格分隔的整数 a1,a2,…,an(0<a1<a2<⋯<an<M)——初始安装的程序 a。
输出格式
Print the only integer — maximum possible total time when the lamp is lit.
输出唯一的整数——灯亮起的最长时间。
输入输出样例
输入#1
3 10 4 6 7
输出#1
8
输入#2
2 12 1 10
输出#2
9
输入#3
2 7 3 4
输出#3
6
说明/提示
In the first example, one of possible optimal solutions is to insert value x=3 before a1, so program will be [3,4,6,7] and time of lamp being lit equals (3−0)+(6−4)+(10−7)=8. Other possible solution is to insert x=5 in appropriate place.
In the second example, there is only one optimal solution: to insert x=2 between a1 and a2. Program will become [1,2,10], and answer will be (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.
在第一个例子中,一种可能的最优解是在 a1 前插入值 x=3,此时程序变为 [3,4,6,7],灯亮的总时间为 (3−0)+(6−4)+(10−7)=8。另一种可能的解是将 x=5 插入合适的位置。
在第二个例子中,仅存在一种最优解:在 a1 和 a2 之间插入 x=2。程序将变为 [1,2,10],答案为 (1−0)+(10−2)=9。
在第三个例子中,最优解是保持程序不变,因此答案为 (3−0)+(7−4)=6。
输入解题思路,AI测评打分。不知道怎么写?