B. Light It Up

 

进行的操作一定在亮区间,从后往前遍历在某区间操作后的收益flag最大就行
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
#pragma optimize O3
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")
#define debug(x) cerr << #x << " : " << x;
const int infint = (ll)1e9;
const ll inf = (ll)1e18;
const int maxn = 100005;
int main()
{
    int n, m, a[maxn], flag = 0, up = 0, maxx = 0;
    a[0] = 0;
    cin >> n >> m;
    a[n+1] = m;
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i];
        if(i % 2 == 1)
        {
            up += (a[i] - a[i-1]);
        }
        else if(i == n && i%2 == 0)
            up += m - a[i];
    }
    for(int i = n; i>0; i--)
    {
        if(i % 2 == 0)
        {
            flag -= (a[i+1] - a[i]);
        }
        else
        {
            flag += (a[i+1] - a[i]);
        }
        maxx = max(flag , maxx);
    }
    if(maxx <= 0) cout << up << endl;
    else cout << up + maxx -1<< endl;



}

猜你喜欢

转载自www.cnblogs.com/mrh-acmer/p/9239543.html