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
The lamp allows only good programs. Good program can be represented as a non-empty array ai must be integers. Of course, preinstalled program is a good program.
The lamp follows program M 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 a.
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 y−x units of time. Segments of time when the lamp is lit are summed up.
First line contains two space separated integers a and the moment when power turns off.
Second line contains a.
Print the only integer — maximum possible total time when the lamp is lit.
3 10
4 6 7
8
2 12
1 10
9
2 7
3 4
6
In the first example, one of possible optimal solutions is to insert value x=5 in appropriate place.
In the second example, there is only one optimal solution: to insert (1−0)+(10−2)=9.
In the third example, optimal answer is to leave program untouched, so answer will be (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; }