Arc123 D
很好考场降智了。
感觉\(D\)比\(C\)好做多了。
考虑\(C_i = A_i - B_i\),那么有\(C_{i + 1} \leq C_i,B_i \leq B_{i + 1},A_{i + 1} - B_{i + 1} \leq A_i - B_i \to B_i + max(0,A_{i + 1} - A_{i}) \leq B_{i+1}\)
因为\(|B_i + C_i| \leq A_i\),所以我们应该让\(B_i,C_i\)为正数,所以我们应让\(B_i\)为最小的数。
所以\(B_i = max(0,B_i + A_{i + 1} - A_i)\)
所以最后\(ans = \sum |d + B_i| +|c - B_i|\),只要选取一个合适的\(B_i\)的就好。
选取中位数。
// Problem: D - Inc, Dec - Decomposition
// Contest: AtCoder - AtCoder Regular Contest 123
// URL: https://atcoder.jp/contests/arc123/tasks/arc123_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#define LL long long
using namespace std;
const int MAXN = 4e5 + 5;
int n, a[MAXN];
LL ans, c[MAXN];
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i ++) scanf("%d", &a[i]);
for(int i = 1; i <= n; i ++) {
if(a[i] >= a[i - 1]) c[i] = c[i - 1] + a[i] - a[i - 1];
else c[i] = c[i - 1];
}
for(int i = n + 1; i <= 2 * n; i ++) c[i] = a[i - n] - c[i - n];
for(int i = 1; i <= n; i ++) c[i] = -c[i];
sort(c + 1, c + 1 + 2 * n);
for(int i = 1; i <= 2 * n; i ++) ans += abs(c[i] - c[n]); printf("%lld", ans);
return 0;
}