904E
$dp$
凉凉.jpg
看到题就想决策单调性,想了一个多小时也没想出来,排名$200+$,$gg$
事实上,我们只可能每$c$个或每一个分一段,假设我们分了一段长为$c$,如果添加一个新元素,如果新的比老的大,没影响,如果小,那么亏损,所以不如不加
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; typedef long long ll; const int N = 1e5 + 5; int n, c; ll ans; int Log[N]; ll a[N][19], dp[N]; ll rmq(int l, int r) { if(l > r) { swap(l, r); } int x = Log[r - l + 1]; return min(a[l][x], a[r - (1 << x) + 1][x]); } int main() { scanf("%d%d", &n, &c); for(int i = 1; i <= n; ++i) { scanf("%lld", &a[i][0]); ans += a[i][0]; } for(int i = 2; i <= n; ++i) { Log[i] = Log[i >> 1] + 1; } for(int j = 1; j <= 18; ++j) { for(int i = 1; i + (1 << j) - 1 <= n; ++i) { a[i][j] = min(a[i][j - 1], a[i + (1 << (j - 1))][j - 1]); } } for(int i = c; i <= n; ++i) { dp[i] = max(dp[i - 1], dp[i - c] + rmq(i, i - c + 1)); } cout << ans - dp[n]; return 0; }