1000B. Light It Up
greedy, 1500, https://codeforces.com/problemset/problem/1000/B
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<a~1~<a~2~<⋯<a~|a|~<M. All a~i~ 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 a~i~ 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 beginning 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.
Input
First line contains two space separated integers n and M (1≤n≤10^5^, 2≤M≤10^9^) — the length of program a and the moment when power turns off.
Second line contains n space separated integers a~1~,a~2~,…,a~n~(0<a~1~<a~2~<⋯<a~n~<M) — initially installed program a.
Output
Print the only integer — maximum possible total time when the lamp is lit.
Examples
input
3 10
4 6 7output
8input
2 12
1 10output
9input
2 7
3 4output
6Note
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 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, 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.
# ref: https://blog.csdn.net/tianwei0822/article/details/80884065
# 分析:加一次操作相当于把某个数字拆成两个,肯定为一开一关,让改变的这个区间长度开灯时间尽可能长,
# 即让开最大(即该数字-1)
# 操作之后的开灯时长为:该数字之前的总开灯时长+该数字之后的总关灯时长+本区间改变之后的开灯时长.
#
# 记录到第i次操作时亮灯时长,记为b[i].
#
# 如本区间长度大于1且为开灯状态,操作后开灯时长为:b[i] + m-a[i]-(b[n+1]-b[i]) - 1,
# 如本区间长度大于1且为关灯状态,操作后开灯时长为:b[i] + m-a[i]-(b[n+1]-b[i]) + a[i]-a[i-1]-1
# 所以遍历一遍维护最大值即可。另外可以不操作,此时开灯时长为b[n+1]。
f = 1 #switch
n, M = map(int, input().split())
a = [0] + [int(x) for x in input().split()] + [M]
b = [0]*(n+2)
for i in range(1,n+2):
b[i] = b[i-1] + f*(a[i]-a[i-1])
f ^= 1 #0->1 or 1->0
ans = b[n+1] #untouched
for i in range(1,n+2):
if (a[i]-a[i-1]>1):
if i&1:
# ans = max(ans, b[i]+M-a[i]-(b[n+1]-b[i])-1)
pass
else:
ans = max(ans, b[i]+a[i]-a[i-1]-1+M-a[i]-(b[n+1]-b[i]))
print(ans)CF 1000B. greedy, 1500, https://codeforces.com/problemset/problem/1000/B
2020fall-cs101-顾臻宜,赵春源,解题思路和源码:
a~k~, 1≤k≤n; b~k~=a~k~-a~k-1~, 1≤k≤n+1, a~0~=0, a~n+1~=M; 若不额外添加开关:ans =
若额外添加开关:由题意,开灯的时间越长越好,所以就算额外添加开关,也必须在一次关灯a~i~的前或后1个时间单位添加。
计算ans:添加处及之前的奇数项,加上添加处之后的(本来的)偶数项,再减去添加处与本来开灯的时间点相差的1个时间单位。
先计算出不插入新操作的答案,再考虑怎么插有可能更新答案。
在位置
经过画图思考知道在一次关灯的前后1个时间单位添加,是等效的,所以只考虑关灯后的下一个时刻(即关灯后的下一时刻是最优的,不会劣于其他情况)。我们枚举位置,计算答案取max即可。
n, m = map(int, input().split())
a = [0] + [int(x) for x in input().split()] + [m]
tot = ans = s = 0
for i in range(1, len(a), 2):
tot += a[i] - a[i-1]
ans = tot #总开灯时间
for i in range(2, len(a), 2):
s += a[i-1] - a[i-2] #前 i 次开灯时间
if a[i] > a[i-1] + 1: #若关灯时长大于 1
t = tot -s #导出后 n-i 次开灯时间
ans = max(ans, s + m-a[i-1]-t - 1) #之前奇数项+之后偶数项-1
print(ans)2022fall-cs101, 姜鑫
思路:算出分割后的时间段,从后往前,用开启时间段减去关闭时间段,取净时间,当净时间最小时,在前面的时间插入一个program,损失一个unit,但是后面的反转了,由净时间知开灯时间变长了,输出原来的开灯时间加上净时间减一即可。
n,m=map(int,input().split())
n+=2
times=[0]+list(map(int,input().split()))+[m,0]
cha=[]
out=0
for i in range(1,n,2):
a=times[i]-times[i-1]
cha.append(a)
out+=a
cha.append(times[i]-times[i+1])
if n%2==0:cha.pop()
dp=[cha[-1]]
for i in range(2,n):
dp.append(dp[i-2]+cha[-i])
mi=min(dp)
if mi <-1:print(out-mi-1)
else:print(out)