Educational Codeforces Round 46 (Rated for Div. 2) 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 yxy−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 (1n1051≤n≤1052M1092≤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
Note

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][3,4,6,7] and time of lamp being lit equals (30)+(64)+(107)=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][1,2,10], and answer will be (10)+(102)=9(1−0)+(10−2)=9.

In the third example, optimal answer is to leave program untouched, so answer will be (30)+(74)=6(3−0)+(7−4)=6.

#include<bits/stdc++.h>

using namespace std;

const int MAXN = 100000 + 10;
int a[MAXN] , b[MAXN] , c[MAXN] ,d[MAXN];

int main()
{
	int n , m ;
	cin >> n >> m;
	a[0] = 0;
	for(int i = 1; i <= n ; i ++)
	{
		cin >> a[i];
	}
	a[n + 1] = m;
	n ++;
	for(int i = 1 ; i <= n ; i ++)
	{
		b[i] = a[i] - a[i - 1];
	}
	int l = 0 , r = 0 , mm = 0;
	for(int i = n ; i >= 1 ; i --)
	{
		if(i & 1)
		{
			l += b[i];
			c[i] = l;
		}
		else
		{
			r += b[i];
			c[i] = r;
		}
	}
	for(int i = 1; i <= n ; i ++)
	{
		if(i & 1)
		{
			mm += b[i];
			d[i] = mm;
		}
		else
		{
			d[i] = d[i-1];
		}
	}
	/*
	cout << "b:  " ;
	for(int i = 1; i <= n ;i ++)
	{
		cout << b[i] << " ";
	}
	cout << endl;
	cout << "c:  ";
	for(int i = 1; i <= n ; i ++)
		cout << c[i] << " ";
	cout << endl;
	cout << "d:  ";
	for(int i = 1; i <= n ; i ++)
		cout << d[i] << " ";
	cout << endl;
	*/
	int maxx = 0 ;
	for(int i = 1; i <= n ; i ++)
	{
		if(a[i] - a[i- 1] == 1)
			continue;
		if(i & 1)
		{
			int t = d[i - 1];
			if(t + b[i] - 1 + c[i + 1] > maxx)
				maxx = b[i] - 1 + c[i + 1] + t;
			//cout << t << " " << maxx << endl;
		}
		else
		{
			int t = d[i - 1];
			//cout << c[i + 2] << endl;
			if(t + b[i] - 1  + c[i + 2] > maxx)
				maxx = b[i] - 1 + c[i + 2] + t;
			//cout << t << " " << maxx << endl;
		}
	}
	//cout << maxx << endl;
	if(c[1] > maxx)
		maxx = c[1];
	cout << maxx;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值