QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#112437 | #6338. Chorus | Scintilla | 0 | 2ms | 3592kb | C++14 | 2.8kb | 2023-06-11 18:47:00 | 2023-06-11 18:47:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, s, e) for (int i = s; i <= e; ++i)
#define drep(i, s, e) for (int i = s; i >= e; --i)
#define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout)
#define pv(a) cout << #a << " = " << a << endl
#define pa(a, l, r) cout << #a " : "; rep(_, l, r) cout << a[_] << ' '; cout << endl
const int inf = 1e18;
const int N = 2e6 + 10;
int read() {
int x = 0, f = 1; char c = getchar();
for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -1;
for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48;
return x * f;
}
int n, m, a[N], pre[N], f[N], g[N], ans = inf;
char s[N];
struct vec {
int x, y;
vec() {}
vec(int _x, int _y) : x(_x), y(_y) {}
friend vec operator - (vec a, vec b) {
return vec(a.x - b.x, a.y - b.y);
}
friend int operator / (vec a, vec b) {
return (__int128) a.x * b.y - (__int128) a.y * b.x;
}
} ;
int s1(int l, int r) { return (l + r) * (r - l + 1) / 2; }
int l1, r1, l2, r2, q2[N];
vec q1[N];
void solve(int w) {
fill(f + 1, f + n + 1, inf);
// rep(i, 1, n) rep(j, 0, n - 1) {
// int val = f[j] + w;
// rep(k, j + 1, i) val += max(0ll, i - a[k]);
// if (val < f[i]) f[i] = val, g[i] = g[j] + 1;
// }
l1 = 1, r1 = 0, q2[l2 = r2 = 1] = 0;
for (int i = 1, p = 0; i <= n; ++ i) {
auto ins = [&](int j) {
// cout << "ins j = " << j << endl;
q1[++ r1] = vec(j, f[j] + pre[j]);
while (l1 + 1 < r1 && (q1[r1] - q1[r1 - 2]) / (q1[r1 - 1] - q1[r1 - 2]) >= 0) {
q1[r1 - 1] = q1[r1], -- r1;
}
} ;
while (p < i && a[p + 1] < i) {
ins(p), ++ p;
}
// cout << "i, p = " << i << ' ' << p << endl;
while (l1 < r1 && (q1[l1 + 1] - q1[l1]) / vec(1, i) >= 0) ++ l1;
// rep(i, l1, r1) cout << q1[i].x << ' ' << q1[i].y << endl;
// cout << endl;
if (l1 <= r1) {
f[i] = q1[l1].y - q1[l1].x * i + w + p * i - pre[p];
g[i] = g[q1[l1].x] + 1;
}
while (l2 <= r2 && q2[l2] < p) ++ l2;
if (l2 <= r2 && f[q2[l2]] + w < f[i]) {
f[i] = f[q2[l2]] + w, g[i] = g[q2[l2]] + 1;
}
while (l2 <= r2 && f[q2[r2]] >= f[i]) -- r2;
q2[++ r2] = i;
if (p > i) ins(i);
}
// pv(w);
// pa(f, 1, n);
// pa(g, 1, n);
if (g[n] >= m) ans = min(ans, f[n] - m * w);
}
signed main() {
n = read(), m = read(), scanf("%s", s + 1);
for (int i = 1, j = 0, k = 0; i <= 2 * n; ++ i) {
if (s[i] == 'A') ++ j;
else a[++ k] = j, pre[k] = pre[k - 1] + a[k];
}
// pa(a, 1, n);
int l = 0, r = n * n;
while (l < r) {
int mid = (l + r) >> 1;
solve(mid);
if (g[n] <= m) r = mid;
else l = mid + 1;
}
solve(l);
ans = min(ans, f[n] - g[n] * l);
printf("%lld\n", ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 16
Accepted
time: 2ms
memory: 3592kb
input:
1 1 BA
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: -16
Wrong Answer
time: 1ms
memory: 3524kb
input:
7 5 ABBAAABBABABBA
output:
2
result:
wrong answer 1st numbers differ - expected: '3', found: '2'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Skipped
Dependency #1:
0%