Submission #882831

# Submission time Handle Problem Language Result Execution time Memory
882831 2023-12-03T20:00:15 Z MilosMilutinovic Chorus (JOI23_chorus) C++14
100 / 100
5729 ms 71900 KB
#include <bits/stdc++.h>
 
using namespace std;
 
struct line {
  long long k, n;
  int f;
  void init() {
    k = 0;
    n = (long long) 1e18;
    f = 0;
  }
  long long val(long long x) {
    return k * x + n;
  }
};
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<int> a, b;
  for (int i = 0; i < 2 * n; i++) {
    if (s[i] == 'A') {
      a.push_back(i);
    } else {
      b.push_back(i);
    }
  }
  vector<int> ia(2 * n);
  for (int i = 0; i < n; i++) {
    ia[a[i]] = i + 1;
  }
  vector<long long> pref(2 * n);
  for (int i = 0; i < 2 * n; i++) {
    if (i > 0) {
      pref[i] = pref[i - 1];
    }
    pref[i] += (s[i] == 'A' ? 1 : 0);
  }
  vector<int> ib(2 * n);
  for (int i = 0; i < n; i++) {
    ib[b[i]] = pref[b[i]];
  }
  for (int i = 0; i < n; i++) {
    if (i > 0) {
      pref[i] = pref[i - 1];
    } else {
      pref[i] = 0;
    }
    pref[i] += ib[b[i]];
  }
  deque<line> hull;
  auto Update = [&](line c) {
    while ((int) hull.size() >= 2) {
      line a = hull.rbegin()[1];
      line b = hull.back();
      long long x_ab = (b.n - a.n) * 1LL * (a.k - c.k);
      long long x_ac = (c.n - a.n) * 1LL * (a.k - b.k);
      if (x_ab > x_ac || (x_ab == x_ac && b.f >= c.f)) {
        hull.pop_back();
      } else {
        break;
      }
    }
    hull.push_back(c);
  };
  auto Query = [&](int x) {
    while ((int) hull.size() >= 2 && (hull.front().val(x) > hull[1].val(x) || 
                                     (hull.front().val(x) == hull[1].val(x) && hull.front().f >= hull[1].f))) {
      hull.pop_front();
    }
    return make_pair(hull.front().val(x), hull.front().f);
  };
  auto Solve = [&](long long mid) {
    const long long inf = (long long) 1e18;
    vector<pair<long long, int>> dp(n + 1, {inf, 0});
    dp[0] = {0, 0};
    int ptr = -1;
    multiset<pair<long long, int>> st;
    hull.clear();
    for (int i = 0; i < n; i++) {
      st.insert(dp[i]);
      while (ptr + 1 <= i && ia[a[i]] >= ib[b[ptr + 1]]) {
        ptr += 1;
        line cur;
        cur.k = -ptr;
        cur.n = dp[ptr].first + (ptr == 0 ? 0 : pref[ptr - 1]) + mid;
        cur.f = dp[ptr].second;
        Update(cur);
        st.erase(st.find(dp[ptr]));
      }
      if (!st.empty()) {
        pair<long long, int> val = *st.begin();
        dp[i + 1] = min(dp[i + 1], {val.first + mid, val.second + 1});
      }
      if (ptr != -1) {
        pair<long long, int> val = Query(ia[a[i]]);
        dp[i + 1] = min(dp[i + 1], {val.first + ia[a[i]] * 1LL * (ptr + 1) - pref[ptr], val.second + 1});
      }
    }
    return dp[n];
  };
  long long low = 0, high = 1e12, p = 0;
  while (low <= high) {
    long long mid = low + high >> 1;
    pair<long long, int> res = Solve(mid);
    if (res.second <= k) {
      p = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  pair<long long, int> res = Solve(p);
  cout << res.first - p * k << '\n';
  return 0;
}

Compilation message

chorus.cpp: In function 'int main()':
chorus.cpp:109:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  109 |     long long mid = low + high >> 1;
      |                     ~~~~^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 2 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 2 ms 480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 2 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 2 ms 480 KB Output is correct
29 Correct 12 ms 604 KB Output is correct
30 Correct 23 ms 604 KB Output is correct
31 Correct 24 ms 600 KB Output is correct
32 Correct 10 ms 604 KB Output is correct
33 Correct 10 ms 604 KB Output is correct
34 Correct 10 ms 604 KB Output is correct
35 Correct 10 ms 812 KB Output is correct
36 Correct 22 ms 600 KB Output is correct
37 Correct 16 ms 604 KB Output is correct
38 Correct 10 ms 808 KB Output is correct
39 Correct 13 ms 796 KB Output is correct
40 Correct 11 ms 768 KB Output is correct
41 Correct 11 ms 760 KB Output is correct
42 Correct 22 ms 964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 2 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 2 ms 480 KB Output is correct
29 Correct 12 ms 604 KB Output is correct
30 Correct 23 ms 604 KB Output is correct
31 Correct 24 ms 600 KB Output is correct
32 Correct 10 ms 604 KB Output is correct
33 Correct 10 ms 604 KB Output is correct
34 Correct 10 ms 604 KB Output is correct
35 Correct 10 ms 812 KB Output is correct
36 Correct 22 ms 600 KB Output is correct
37 Correct 16 ms 604 KB Output is correct
38 Correct 10 ms 808 KB Output is correct
39 Correct 13 ms 796 KB Output is correct
40 Correct 11 ms 768 KB Output is correct
41 Correct 11 ms 760 KB Output is correct
42 Correct 22 ms 964 KB Output is correct
43 Correct 308 ms 4416 KB Output is correct
44 Correct 495 ms 6836 KB Output is correct
45 Correct 527 ms 6988 KB Output is correct
46 Correct 214 ms 7360 KB Output is correct
47 Correct 188 ms 7072 KB Output is correct
48 Correct 202 ms 7424 KB Output is correct
49 Correct 191 ms 7220 KB Output is correct
50 Correct 525 ms 6956 KB Output is correct
51 Correct 497 ms 6924 KB Output is correct
52 Correct 199 ms 7436 KB Output is correct
53 Correct 210 ms 6964 KB Output is correct
54 Correct 356 ms 6964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 2 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 2 ms 480 KB Output is correct
29 Correct 12 ms 604 KB Output is correct
30 Correct 23 ms 604 KB Output is correct
31 Correct 24 ms 600 KB Output is correct
32 Correct 10 ms 604 KB Output is correct
33 Correct 10 ms 604 KB Output is correct
34 Correct 10 ms 604 KB Output is correct
35 Correct 10 ms 812 KB Output is correct
36 Correct 22 ms 600 KB Output is correct
37 Correct 16 ms 604 KB Output is correct
38 Correct 10 ms 808 KB Output is correct
39 Correct 13 ms 796 KB Output is correct
40 Correct 11 ms 768 KB Output is correct
41 Correct 11 ms 760 KB Output is correct
42 Correct 22 ms 964 KB Output is correct
43 Correct 308 ms 4416 KB Output is correct
44 Correct 495 ms 6836 KB Output is correct
45 Correct 527 ms 6988 KB Output is correct
46 Correct 214 ms 7360 KB Output is correct
47 Correct 188 ms 7072 KB Output is correct
48 Correct 202 ms 7424 KB Output is correct
49 Correct 191 ms 7220 KB Output is correct
50 Correct 525 ms 6956 KB Output is correct
51 Correct 497 ms 6924 KB Output is correct
52 Correct 199 ms 7436 KB Output is correct
53 Correct 210 ms 6964 KB Output is correct
54 Correct 356 ms 6964 KB Output is correct
55 Correct 3532 ms 42624 KB Output is correct
56 Correct 3552 ms 69352 KB Output is correct
57 Correct 5729 ms 68380 KB Output is correct
58 Correct 2005 ms 71308 KB Output is correct
59 Correct 2045 ms 71588 KB Output is correct
60 Correct 2072 ms 71900 KB Output is correct
61 Correct 1971 ms 71440 KB Output is correct
62 Correct 5381 ms 68380 KB Output is correct
63 Correct 5428 ms 68292 KB Output is correct
64 Correct 2040 ms 71328 KB Output is correct
65 Correct 2119 ms 71332 KB Output is correct
66 Correct 3658 ms 69028 KB Output is correct
67 Correct 2110 ms 71324 KB Output is correct