CodeForces ~ 1000B ~ Light It Up(思维,贪心,模拟)

版权声明:欢迎加好友讨论~ QQ1336347798 https://blog.csdn.net/henu_xujiu/article/details/81484925
题目链接

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.

Input

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

Output

Print the only integer — maximum possible total time when the lamp is lit.

Input

3 10
4 6 7

Output

8

Input

2 12
1 10

Output

9

Input

2 7
3 4

Output

6

Note

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

一个模拟思维题。就是有一盏灯,0时刻开着。n次操作,你可以在其中加入一次操作(或者不加),操作为:a[i]时刻按一下开关,状态变为相反状态(开->关or关->开)。问灯亮着的时长最长为多少?

样例一0~4开(时长为4-0),4~6关(时长为6-4),6~7开(时长为7-6),7~10关(时长为10-7),可以这这些时间点中加操作使开灯时间变长 在3处加操作,开灯时长变为0~3(开)3~4(关)4~6(开)6~7(关)7~10(开)

加操作后开灯时间改变    开灯时长为:l[i]+m-a[i]-(l[n+1]-l[i])-1遍历维护即可具体看代码注释

ac代码

#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; 
	 
}

	

猜你喜欢

转载自blog.csdn.net/henu_xujiu/article/details/81484925