codeforces1000b csdn-博客

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| < M. All ai 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 = 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 a1a1 and a2a2. 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.

  • 题意:给你一个长度为n的时刻数组,和最大时刻m。有一个灯,这个时钟从0时开始亮,然后没经过一个数组上的点,亮灭会翻转一次。在你可以在任意时刻加入一个时间,求这个灯亮最大时间

  • 解题思路:用两个数组a存时间,b存不加的情况下从头但现在灯亮的时间。然后就会可以求出一个规律,在i时的区间附件插入短点的话那么之后时间就会变成
    t = 2 * b[i] + m - a[i] - b[n+1] - 1;应为可以发现加完点后这个点后面的亮灭顺序会完全翻转一次,而前面的不会翻转。
    公式解释:
    m - b[n+1] 的意思为恰好把亮灭权给翻转了一次,然后 结果记为s1
    s1 + b[i] ==a[i] 所以还要在减去a[i] 结果记为s2
    s2 + b[i] 又因为前面的顺序不变,所以还有在加上一个b[i]
    最后还要在减去一个1 , 这个大家可以取试一试会更清楚 O(∩_∩)O!
    贴上代码:

#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<cstring>
#include<utility>
#define endl '\n'
#define _ ios::sync_with_stdio(false)
bool SUBMIT = 1;
typedef long long ll;
using namespace std;
const int inf = 2e5+100;
int n,m,a[inf],b[inf];
int main()
{
    //if(!SUBMIT)freopen("i.txt","r",stdin);else _;
    cin>>n>>m;
    bool f = true;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        b[i]+=b[i-1]+f*(a[i]-a[i-1]);
        f=!f;
    }
    b[n+1]=b[n]+f*(m-a[n]);
    int ans=b[n+1];
    for(int i=1;i<=n;i++)
    {
        ans=max(ans,2*b[i]+m-b[n+1]-a[i]-1);
    }
    cout<<ans<<endl;
    return 0;
}

有喜欢我博客的朋友可以给波关注哈O(∩_∩)O哈!

猜你喜欢

转载自blog.csdn.net/qq_38701476/article/details/80882211
今日推荐