QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#112440#6338. ChorusScintilla16 3ms11848kbC++142.7kb2023-06-11 18:56:522023-06-11 18:56:54

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-11 18:56:54]
  • 评测
  • 测评结果:16
  • 用时:3ms
  • 内存:11848kb
  • [2023-06-11 18:56:52]
  • 提交

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];
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);
}

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 = 1, r = n * n;
  while (l < r) {
    int mid = (l + r) >> 1;
    solve(mid + 1);
    if (g[n] < m) r = mid;
    else l = mid + 1;
  }
  solve(l);
  printf("%lld\n", f[n] - m * l);
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 16
Accepted

Test #1:

score: 16
Accepted
time: 1ms
memory: 9720kb

input:

1 1
BA

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 1ms
memory: 9856kb

input:

7 5
ABBAAABBABABBA

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: 0
Accepted
time: 0ms
memory: 9852kb

input:

10 3
BABBABAABAABBABBBAAA

output:

26

result:

ok 1 number(s): "26"

Test #4:

score: 0
Accepted
time: 1ms
memory: 9648kb

input:

10 2
AAABBABABBAAABBBAABB

output:

11

result:

ok 1 number(s): "11"

Test #5:

score: 0
Accepted
time: 1ms
memory: 9732kb

input:

10 1
BBBBBBBBBBAAAAAAAAAA

output:

100

result:

ok 1 number(s): "100"

Test #6:

score: 0
Accepted
time: 1ms
memory: 11848kb

input:

10 2
BBBBBBBBBBAAAAAAAAAA

output:

75

result:

ok 1 number(s): "75"

Test #7:

score: 0
Accepted
time: 1ms
memory: 9732kb

input:

10 9
BBBBBBBBBBAAAAAAAAAA

output:

56

result:

ok 1 number(s): "56"

Test #8:

score: 0
Accepted
time: 3ms
memory: 9720kb

input:

10 10
BBBBBBBBBBAAAAAAAAAA

output:

55

result:

ok 1 number(s): "55"

Test #9:

score: 0
Accepted
time: 2ms
memory: 9852kb

input:

10 10
ABABABABABABABABABAB

output:

0

result:

ok 1 number(s): "0"

Test #10:

score: 0
Accepted
time: 0ms
memory: 11764kb

input:

10 2
ABAAABABABBBABABABAB

output:

14

result:

ok 1 number(s): "14"

Test #11:

score: 0
Accepted
time: 0ms
memory: 9684kb

input:

10 4
ABAABBAAABBBAAABBBAB

output:

2

result:

ok 1 number(s): "2"

Test #12:

score: 0
Accepted
time: 3ms
memory: 11640kb

input:

10 4
ABAAABBBAAABBBAABBAB

output:

2

result:

ok 1 number(s): "2"

Subtask #2:

score: 0
Wrong Answer

Dependency #1:

100%
Accepted

Test #13:

score: 0
Wrong Answer
time: 3ms
memory: 9692kb

input:

179 54
AAABABABABBAAABBABBABBABBBAAABAAAAABBBABAAAAABABBBAABBBABABBAABABAABABBBBABAABAABABABBBABBAABABBAABBAABABBAAABAAAAAAAABBAAAAABAAABBBBBBBABBAABBBABABAABBAABBABABABBABAAABABAAABABABBAABABAAABBABABABABABBAAABABBBBBBBAABBBAABABBBBABAABBAAAABAABBABABAABAAABABAAAABBBAABAAABBABABBBABAAABAABBBABBBBBA...

output:

39

result:

wrong answer 1st numbers differ - expected: '41', found: '39'

Subtask #3:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%

Subtask #5:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%