Codeforces1000B——Light It Up

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 0 and turn power off at moment M. 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 a, where 0

#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN=100005;
int n,m;
int a[MAXN];
int on[MAXN];
int off[MAXN];
int main(void){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    a[n+1]=m;
    for(int i=1;i<=n+1;i++){
        if(i%2){
            on[i]=on[i-1]+a[i]-a[i-1];
            off[i]=off[i-1];
        }
        else{
            on[i]=on[i-1];
            off[i]=off[i-1]+a[i]-a[i-1];
        }
    }
    int ans=on[n+1];
    for(int i=1;i<=n;i++){
        int tmp=0;
        //这个点左或右可以插入
        if(a[i]-a[i-1]>1 || a[i+1]-a[i]>1){
            if(i%2){
                //插入在i点的左边 (前面开的时间少了1)
                tmp=on[i]-1+off[n+1]-off[i];
                //插入在i点的右边 (后面关的时间多了1)
                //tmp=on[i]+off[n+1]-off[i]-1;
            }
            else{
                //插入在i点的左边 (前面关的时间少了1)
                tmp=on[i]+1+off[n+1]-off[i];
                //插入在i点的右边 (后面关的时间少了1,即开的时间多了1)
                //tmp=on[i]+off[n+1]-off[i]+1;
            }
        }
        ans=max(ans,tmp);
    }
    printf("%d\n",ans);
    return 0;
}
全部评论

相关推荐

链接
海梨花:我说话难听,你这简历跟没写没啥区别,搜搜别人的简历,用心写,不要随随便便就结束了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务