Educational Codeforces Round 46 (Rated for Div. 2) B. Light It Up

Bryce1010模板

http://codeforces.com/problemset/problem/1000/B

思路:先用两个数组sumon[]和sumoff[]将亮着的灯和灭的灯累计一下。
然后从左到右扫描插入一个开关,取得到的最大值。

#include
using namespace std;

#define ll long long
const int MAXN=1e5+10;
ll sumon[MAXN];
ll sumoff[MAXN];
ll a[MAXN];
int main()
{
    ll n,m;
    cin>>n>>m;
    int i;
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
        if(i&1)
        {
            sumon[i]=sumon[i-2]+a[i]-a[i-1];
            sumoff[i]=sumoff[i-1];
            //cout<<"on:"<
        }
        else
        {
            sumoff[i]=sumoff[i-2]+a[i]-a[i-1];
            sumon[i]=sumon[i-1];
            //cout<<"off:"<
        }
    }
    //i++;
    if(n&1)
    {
        sumoff[i]=sumoff[i-2]+m-a[i-1];
        sumon[i]=sumon[i-1];
        //cout<<"i-2"<
        //cout<<"off:"<
    }
    else
    {
        sumon[i]=sumon[i-2]+m-a[i-1];
        sumoff[i]=sumoff[i-1];
        //cout<<"on:"<
    }
    //cout<
    ll maxlit=-1;
    ll lit;
    maxlit=sumon[n+1];
    for(i=1;i<=n+1;i++)
    {
        lit=-1;
        if(i&1)
        {
            lit=sumon[i]-1+sumoff[n+1]-sumoff[i-1];
            maxlit=max(maxlit,lit);
        }
        else
        {
            lit=sumon[i-1]+sumoff[n+1]-sumoff[i-1]-1;
            maxlit=max(lit,maxlit);
        }
    }
    cout<return 0;
}

你可能感兴趣的:(Educational Codeforces Round 46 (Rated for Div. 2) B. Light It Up)