CodeForces - 1000B ( Light It Up)(规律下的开关灯)

题目链接:http://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 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

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=3x=3 before a1a1, so program will be [3,4,6,7][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][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.

题目大意:在0~m这个区间内,有n次操作,一开始的时候灯是亮着的,每操作一次,会将灯熄灭/点亮,现在对于这个已知的操作顺序,在多加一次/不加操作,找出灯能亮的最长时间,输出最长的时间

对于加一个开关,会改变后面所有的亮,灭的情况,因此,加一个开关后,后面所有灭的灯会都亮,因此开两个数组,一个记录亮的,一个记录灭的,对于每个开关处判断一下即可

ac:

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 1000000007
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//std::ios::sync_with_stdio(false);
	//			记录开的 	记录关的 
int arr[100100],op[100100],clo[100100];
ll now,ans,sum;

int main()
{
	ll n,m;
	cin>>n>>m;
	for(int i=1;i<=n;++i)
	{
		cin>>arr[i];
		op[i]=op[i-1];
		clo[i]=clo[i-1];
		if(i&1)//奇  记录开灯 
		{
			op[i]=op[i]+arr[i]-arr[i-1];
			sum=sum+arr[i]-arr[i-1];
		}
		else//偶  记关灯 
			clo[i]=clo[i]+arr[i]-arr[i-1];
	}
	if(n&1)//最后是关灯 
	{
		op[n+1]=op[n];
		clo[n+1]=clo[n]+m-arr[n];
	}
	else//最后是开灯 
	{
		op[n+1]=op[n]+m-arr[n];
		clo[n+1]=clo[n];
		sum=sum+m-arr[n];
	}
	for(int i=1;i<=n;++i)
	{//在第i个开关后插入一个开关,后面的所有关灯变为开灯,此处关了一秒(-1) 
		now=op[i]+clo[n+1]-clo[i]-1;
		ans=max(now,ans);//取最大值 
	}
	ans=max(ans,sum);
	cout<<ans<<endl;
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值