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
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间插入数字使得相邻两数相差值的和最大

分析:要使插入的数能够让差值和最大则插入的位置肯定是每个数减一的位置,接下来在插入的时候求个前缀和,后缀和就行了

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e5 + ;
const int mod = 1e9 + ;
typedef long long ll;
ll a[maxn], n, m;
int main(){
std::ios::sync_with_stdio(false);
while( cin >> n >> m ) {
ll ans = , on = , off = ;
for( ll i = ; i <= n; i ++ ) {
cin >> a[i];
}
a[] = , a[n+] = m;
for( ll i = n; i >= ; i -- ) {
if( i& ) {
off += a[i+] - a[i] - ;
} else {
on += a[i+] - a[i] - ;
}
if( a[i+] != a[i] + ) {
ans = max( ans, off - on );
}
if( i& ) {
off ++;
} else {
on ++;
}
}
cout << ans + on << endl;
}
return ;
}

Light It Up CF1000B 思维的更多相关文章

  1. 思维+模拟--POJ 1013 Counterfeit Dollar

    Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver d ...

  2. Codeforces | CF1000B 【Light It Up】

    蒟蒻第二篇题解... 比赛的时候写这道题MLE了qwq..根据CF的赛制我也没敢再交第二次.. 简单讲一下思路好了(假装是dalao)..根据题意要加一个或者不加新的点..如果加一个新的点意味着从这个 ...

  3. B. Light It Up 思维题

    Recently, you bought a brand new smart lamp with programming features. At first, you set up a schedu ...

  4. CodeForces 1000B Light It Up(贪心、思维)

    https://codeforces.com/problemset/problem/1000/B 题意: 一个模拟思维题.就是有一盏灯,0时刻开着.n次操作,你可以在其中加入一次操作(或者不加),操作 ...

  5. Gym 101775C - Traffic Light - [思维题]

    题目链接:http://codeforces.com/gym/101775/problem/C 题意: 给出 $N$ 个红绿灯,又给出 $N+1$ 个距离 $S_i = S_0,S_1, \cdots ...

  6. hdu 3698 Let the light guide us(线段树优化&简单DP)

    Let the light guide us Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/O ...

  7. (纪录片)光的故事 BBC Light Fantastic (2004)

    简介: 导演: Jeremy Turner主演: Simon Schaffer / Dimitri Andreas ... Al Hazen / Edmund Dehn ... Priest/Old ...

  8. [C#][算法] 用菜鸟的思维学习算法 -- 马桶排序、冒泡排序和快速排序

    用菜鸟的思维学习算法 -- 马桶排序.冒泡排序和快速排序 [博主]反骨仔 [来源]http://www.cnblogs.com/liqingwen/p/4994261.html  目录 马桶排序(令人 ...

  9. Photoshop、Illustrator思维导图笔记

    半年前学习Photoshop时记得的思维导图笔记,可能不是很全,常用的基本都记下了.

随机推荐

  1. 有趣的Flex布局

    对于刚接触前端的小白,在还原页面样式的时候,往往会遇到页面布局(layout)上的问题,用着生硬的padding来固定盒子的位置,不仅代码看的沉重,还得适应各种浏览器页面,始终没有办法做到统一.接下来 ...

  2. Linux下,为应用程序添加桌面图标(ubuntu18.4)

    一.桌面图标位置 Lniux下桌面图标储存路径为:/usr/share/applications 二.桌面图标格式 所有桌面图标格式均为desktop,即名为XXX.desktop 三.编辑内容(常用 ...

  3. Extjs4 combobox autoLoad: true 时,加载两次

    问题是这样的,combobox 远程加载数据时,当我们把 store 设置为  autoLoad: tue, 时,这样页面加载时,store 会load 一次,但是我们在第一次点击 下来框时,他还会 ...

  4. Java中只有值传递,(及值传递与引用传递详解)

    首先呢,我们来说一下值传递与引用传递的区别(这两个玩意儿实在调用函数的时候提到的) 比如说 code( a) code( int a ) code(a)是调用函数,a是我们原本函数的一个值类型,然后使 ...

  5. 算法与数据结构基础 - 二叉树(Binary Tree)

    二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如Leet ...

  6. 从MySQL迁移到MariaDB(CentOS)

    MySQL是世界上最流行的开源关系数据库.原来 MariaDB 的设计初衷之一就是完全兼容 MySQL,包括 API 和客户端协议,使之能轻松成为 MySQL 的代替品.MariaDB 与 MySQL ...

  7. 洛谷 P2016 战略游戏

    题意简述简述 求一棵树的最小点覆盖 题解思路 树形DP dp[i][0]表示第i个点覆盖以i为根的子树的最小值,且第i个点不放士兵 dp[i][1]表示第i个点覆盖以i为根的子树的最小值,且第i个点放 ...

  8. 强化学习(Reinforcement Learning)中的Q-Learning、DQN,面试看这篇就够了!

    1. 什么是强化学习 其他许多机器学习算法中学习器都是学得怎样做,而强化学习(Reinforcement Learning, RL)是在尝试的过程中学习到在特定的情境下选择哪种行动可以得到最大的回报. ...

  9. 服务注册组件——Eureka高可用集群搭建

    服务注册组件--Eureka高可用集群搭建 什么是Eureka? 服务注册组件:将微服务注册到Eureka中. 为什么需要服务注册? 微服务开发重点在一个"微"字,大型应用拆分成微 ...

  10. R 包 rgl 安装失败, 报错 X11 not found but required, configure aborted 及解决方法

    R 包 rgl 安装失败, X11 not found but required, configure aborted * installing *source* package ‘rgl’ ... ...