CodeForces - 1000B ( Light It Up)开关灯 模拟 ʕ •ᴥ•ʔ

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 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.

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=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

题意:灯的程序由一个递增数组a表示,每一个时刻表示在该时刻灯的状态发生变化。0时刻灯打开,m时刻灯关闭,程序结束。问a数组中插入最多一个,使数组仍有序,且满足a[i-1]<a[i]<a[i+1],问灯打开的最长时间为多少。

思路:先记录到ai时刻开的时长和关的时长。然后发现,若使开的时间最长,插入的x只能为a[i]+1或a[i]-1,i为任意值。所以,进行模拟,以求得最长时间。代码如下:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long 
ll up[100010];
ll on[100010];
ll a[100010];
int main()
{
	int n,m;
	cin>>n>>m;
	int flag=1;
	a[0]=0;
	ll ans=0;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		up[i]=up[i-1];// up 存的是之前所有的关灯时间 
		on[i]=on[i-1];// on 存的是之前所有的开灯时间 
		if(flag&1)// i为奇数的时候 将要关灯 
		{
			on[i]+=a[i]-a[i-1];
			ans+=a[i]-a[i-1];// 计算一开始总的开灯时间 
		}
		else
		{
			up[i]+=a[i]-a[i-1];
		}
		flag++;
	}
	a[n+1]=m;
	if(n&1)//n为奇 关灯 
	{
		on[n+1]=on[n];
		up[n+1]=up[n]+a[n+1]-a[n];
	}
	else //n为偶 开灯 
	{
		up[n+1]=up[n];
		on[n+1]=on[n]+a[n+1]-a[n];
		ans+=a[n+1]-a[n];
	}
	ll maxx=0;
	for(int i=1;i<=n;i++)
	{
		//在第i个开关后插入一个开关,后面的所有关灯变为开灯,此处关了一秒(-1) 
		ll h=0;
		h=on[i]+up[n+1]-up[i]-1;
		maxx=max(maxx,h);
	}
	maxx=max(maxx,ans);
	cout<<maxx<<endl;
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/henucm/article/details/81540207