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

题意:输入n,m;

有n个数a[i],表示在a[i]时刻触动开关,如果为打开状态就变成关闭,否则就变成打开

现在,给你一次机会,让你在某时刻触动开关,使开灯时间在【0,m】内变成最大开灯时间,输出开灯时间

要想时间最大,触动开关的时刻肯定与a[i]相邻 ,no why

第一步,输入数据,反转开关数据,用数组a储存起来,首位值为a[1],初始化a【0】=0;

令a[n+1]=m;

n++;

第二步,计算时间,用数组b储存在a[i]时刻开灯时间,数组c储存aj[i]时间的关灯时间,

然后遍历i,(1<=i<=n-1)

在i处,开灯时间t=b[i]+c[n]-c[i]-1

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<cstring>
using namespace std;
#define max 100003
#define mx 1e9+8
int a[max],b[max],c[max];///a���鴢�濪�ص�ʱ��
///b���鴢�濪��ʱ��
///c���鴢��ص�ʱ��
int n,m;

int main()
{
      ios::sync_with_stdio(false);


      cin>>n>>m;
      for(int i=1;i<=n;i++)
      {
            cin>>a[i];
      }
      a[n+1]=m;
      n++;
      for(int i=1;i<=n;i++)
      {
            if(i&1)
            {
                  b[i]=b[i-1]+a[i]-a[i-1];
                  c[i]=c[i-1];
            }
            else
            {
                  c[i]=c[i-1]+a[i]-a[i-1];
                  b[i]=b[i-1];
            }
      }
      // for(int i=1;i<=n;i++)
      // {
      //       printf("%d    %d   %d\n",a[i],b[i],c[i] );
      // }
      //n++;

      // if(n&1)
      // {
      //       c[n+1]=c[n]+a[n]-a[i-1];
      //       b[i]=b[i-1];
      // }
      // else
      // {
      //
      // }
      long long ans=b[n];
      for(int i=1;i<=n;i++)
      {
            if(ans<b[i]+c[n]-c[i]-1)
            {
                  ans=b[i]+c[n]-c[i]-1;
            }
      }
      cout<<ans<<endl;
      return 0;
}

然后计算最大值

猜你喜欢

转载自blog.csdn.net/henu_1710252529/article/details/81590337