题意
Sol
比较套路的一个题。
第一问二分答案check一下
第二问设(f[i][j])表示前(i)个数,切了(j)段的方案数,单调队列优化一下。
转移的时候只需要保证当前段的长度小于最大限度即可。
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 50001, mod = 10007;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-')f =- 1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, a[MAXN], sum[MAXN];
int check(int val) {
int tot = 0, pre = 0;
for(int i = 1; i <= N; i++) {
if(sum[i] - sum[pre] > val) {
if((sum[i - 1] - sum[pre] <= val) && a[i] <= val) tot++, pre = i - 1;
else return 0;
}
}
return tot <= M;
}
int solve1() {
int l = 0, r = sum[N], ans;//二分最大长度
while(l <= r) {
int mid = l + r >> 1;
if(check(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
return printf("%d ", ans), ans;
}
int add(int x, int y) {
if(x + y < 0) return x + y + mod;
else return (x + y > mod ? x + y - mod : x + y);
}
int solve2(int mx) {
static int f[MAXN], g[MAXN], q[MAXN], ans = 0;
// f[0][0] = 1;
for(int i = 1; i <= N; i++) g[i] = (sum[i] <= mx);
for(int j = 1; j <= M; j++) {
for(int i = 1, h = 1, t = 0, s = 0; i <= N; i++) {
while(h <= t && (sum[i] - sum[q[h]] > mx)) s = add(s, -g[q[h++]]);
q[++t] = i;
f[i] = s; s = add(s, g[i]);
/* for(int k = i - 1; k >= 1; k--) {
if(sum[i] - sum[k] <= mx) (f[i][j] += f[k][j - 1]) %= mod;
else break;
}*/
}
ans = add(ans, f[N]);
memcpy(g, f, sizeof(g));
}
return ans % mod;
}
int main() {
// freopen("10.in", "r", stdin);
N = read(); M = read();
for(int i = 1; i <= N; i++) a[i] = read(), sum[i] = sum[i - 1] + a[i];
printf("%d", solve2(solve1()));
}