Codeforces educational round 46 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. 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≤105, 2M1092≤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) — 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
#include
#include
#include//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include
#include
#include
#include
#include
#include
#define maxn 100005
#define UB (maxn*64)
#define ll __int64
#define INF 10000000
using namespace std;
int tim[maxn],seq[maxn];
/*
题目大意:具体的题目意思我也没看,实际概念而已,
再加上考翻译,,很浪费时间,,直接看数据,并通过简单的题目浏览去理解,
一般没啥问题(英语差的人的苟且偷生)。

给定n个数,和上界m,
即在0到m个区间内插入n个数,形成n+1个区间,
区间长度提取出来单独成一个序列。

可以选择其中一个数分裂,(x=y+z),
分裂后的奇数序和(tim[1]+tim[3]+....)
问最大化是多少。

贪心在于,拆分肯定是1和n-1(n本身为1可以略过)
*/
//long long compute(int x,int y)
//{
//    if(x>y) return 0;
//    long long ans=0;
//    while(x<=y)
//    {
//        ans+=tim[x];
//         x+=2;
//    }
//    return ans;
//}
long long tot;
int main()
{
    int n,m;
    cin>>n>>m;
    seq[1]=0;
    for(int i=1;i<=n;i++) cin>>seq[i];
    seq[n+1]=m;
    long long tp,ans=0;
    long long oddsum=0,totsum=0,tmp,tmpans;
    for(int i=1;i<=n+1;i++)
    {
        tim[i]=seq[i]-seq[i-1];
        if(i&1) ans+=tim[i];
        tot+=tim[i];///总和
    }
    tmpans=ans;///奇数和
    for(int i=1;i<=n+1;i++)
    {
       if ( i & 1 ) oddsum += tim [i];///遍历中维护的奇数和

        totsum += tim[i];///遍历中维护的总数和
        tp = 0;
        tmp = tmpans - oddsum;///i+1~n+1的奇数和

        if( i & 1 )   tp = tot-totsum - tmp ;
        else    tp = tmp;///如果是偶数,则应取i+2~n+1的奇数和,和i+1~n+1奇数和互补,即和固定。
        ///(该常数和可以通过维护的变量求出)

        ans=max(ans,tp-1+oddsum);///式子经过化简,oddsum里面包含了tim[i],贪心思想体现在,,如果tim[i]要分裂,
        ///那么一定会分裂成1和tim[i]-1。
        //else    ans=max(ans,tp-1+oddsum);
    }
    cout<

你可能感兴趣的:(Codeforces习题集,贪心策略/决策问题)