# [BZOJ 2151]种树

## Description

$1\leq n\leq 200000$

## Solution

[APIO/CTSC 2007]数据备份有点类似。

## Code

#include <bits/stdc++.h>
using namespace std;
const int N = 200000+5, inf = 1e9;

int n, m, f[N][2][2], a[N], g[N][2][2], tmp;

bool check(int mid) {
f[1][1][1] = a[1]-mid, g[1][1][1] = 1;
f[1][0][0] = 0, g[1][0][0] = 0;
f[1][1][0] = f[1][0][1] = -inf;
for (int i = 2; i <= n; i++)
for (int j = 0; j <= 1; j++) {
f[i][j][1] = f[i-1][j][0]+a[i]-mid;
g[i][j][1] = g[i-1][j][0]+1;
if (f[i-1][j][0] > f[i-1][j][1] || (f[i-1][j][0] == f[i-1][j][1] && g[i-1][j][0] < g[i-1][j][1]))
f[i][j][0] = f[i-1][j][0], g[i][j][0] = g[i-1][j][0];
else f[i][j][0] = f[i-1][j][1], g[i][j][0] = g[i-1][j][1];
}
int mf = -inf, mg = 0;
for (int i = 0; i <= 1; i++)
for (int j = 0; j <= 1; j++)
if (!(i&j))
if (f[n][i][j] > mf || (f[n][i][j] == mf && g[n][i][j] < mg))
mf = f[n][i][j], mg = g[n][i][j];
tmp = mf+m*mid;
return mg <= m;
}
int main() {
scanf("%d%d", &n, &m);
if (n/2 < m) {puts("Error!"); return 0; }
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
int l = -1000, r = 1000, ans = -1, mid;
while (l <= r) {
mid = (l+r)>>1;
if (check(mid)) r = mid-1, ans = tmp;
else l = mid+1;
}
printf("%d\n", ans);
return 0;
}
posted @ 2020-08-17 00:19  NaVi_Awson  阅读(80)  评论(0编辑  收藏  举报