在这个数组下
10 1
103 4 3 4 882 20 4 2 99 4
最终我二分的 r=−256966,但是最后 chk 能在 b 后面输出 x,但是不能输出 c。。
#include <bits/stdc++.h>
#define PR(x) printf(x ? "YES\n" : "NO\n")
#define pr(x) printf(x ? "Yes\n" : "No\n")
#define mk make_pair
#define pb emplace_back
#define fi first
#define se second
#define pii pair<int, int>
#define fore(i, u) for (int i = h[u]; i; i = e[i].nxt)
#define all(x) x.begin(), x.end()
#define int long long
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (! isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }
while (isdigit(ch)) { x = x * 10 + ch - 48; ch = getchar(); }
return x * f;
}
using db = double;
const int maxn = 1e5 + 10, inf = 1e9;
int h, t, n, m, s[maxn], f[maxn], q[maxn], g[maxn];
db X(int i) { return s[i]; }
db Y(int i) { return s[i] * s[i] + f[i]; }
db slp(int x, int y) { return (Y(x) - Y(y)) / (X(x) - X(y)); }
void chk(int x) {
q[h = t = 1] = 0;
for (int i = 1; i <= n; i ++) {
while (h < t && slp(q[h], q[h + 1]) < 2 * (s[i] + 1)) h ++;
f[i] = f[q[h]] + (s[i] - s[q[h]] + 1) * (s[i] - s[q[h]] + 1) - x;
g[i] = g[q[h]] + 1;
while (h < t && slp(q[t - 1], q[t]) > slp(q[t], i)) t --;
q[++ t] = i;
}
// ------------------------------------
if (x == -256966) cout << 'x' << endl;
}
signed main() {
n = read(), m = read();
for (int i = 1; i <= n; i ++) s[i] = read() + s[i - 1];
int l = -inf, r = inf;
while (l + 1 < r) {
int mi = (l + r) / 2; chk(mi);
if (g[n] <= m) l = mi; else r = mi;
}
// ------------------------------------
cout << 'b', chk(r), cout << 'c'; if (g[r] >= m) printf("%lld", f[n] + g[n] * r);
else chk(l), printf("%lld", f[n] + g[n] * l);
return 0;
}