zoukankan      html  css  js  c++  java
  • Light It Up CF1000B 思维

    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<a1<a2<⋯<a|a|<M. 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<a1<a2<⋯<an<M) — 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.

    题意:在0-m间插入数字使得相邻两数相差值的和最大

    分析:要使插入的数能够让差值和最大则插入的位置肯定是每个数减一的位置,接下来在插入的时候求个前缀和,后缀和就行了

    #include <map>
    #include <set>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define debug(a) cout << #a << " " << a << endl
    using namespace std;
    const int maxn = 1e5 + 10;
    const int mod = 1e9 + 7;
    typedef long long ll;
    ll a[maxn], n, m;
    int main(){
        std::ios::sync_with_stdio(false);
        while( cin >> n >> m ) {
            ll ans = 0, on = 0, off = 0;
            for( ll i = 1; i <= n; i ++ ) {
                cin >> a[i];
            }
            a[0] = 0, a[n+1] = m;
            for( ll i = n; i >= 0; i -- ) {
                if( i&1 ) {
                    off += a[i+1] - a[i] - 1;
                } else {
                    on += a[i+1] - a[i] - 1;
                }
                if( a[i+1] != a[i] + 1 ) {
                    ans = max( ans, off - on );
                }
                if( i&1 ) {
                    off ++;
                } else {
                    on ++;
                }
            }
            cout << ans + on << endl;
        }
        return 0;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    html5 canvas雨点打到窗玻璃动画
    html5跟随鼠标炫酷网站引导页动画特效
    如何实现复选框的全选和取消全选效果
    CSS3透明属性opacity
    jQuery实现方式不一样的跳转到底部
    ul li设置横排,并除去li前的圆点
    jQuery美女幻灯相册轮播源代码
    微软modern.IE网站,多版本IE免费测试工具集
    css中position与z-index
    C#一个方法返回多个值
  • 原文地址:https://www.cnblogs.com/l609929321/p/9310281.html
Copyright © 2011-2022 走看看