Submission #1042767

# Submission time Handle Problem Language Result Execution time Memory
1042767 2024-08-03T11:01:14 Z abczz Chorus (JOI23_chorus) C++17
100 / 100
1760 ms 63960 KB
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <array>
#include <cmath>
#define ll long long
#define ld long double

using namespace std;

ll n, k, l, r, mid, dp[1000000];
ll tot[1000000];
ld ep = 1e-8;
vector <ll> A, B, nxA, ps;
string S, T;
struct Line{
  ll m; 
  ll c;
  ll x;
};
ld intx(Line I, Line J) {
  return (ld)(I.c-J.c)/(J.m-I.m);
}
ll C(ll l, ll r) {
  if (nxA[l] > r) return 0;
  ll nx = max(l, nxA[l]), cnt = r-nx+1, cnt2 = max(0LL, l-nxA[l]);
  return ps[r]-(nx ? ps[nx-1] : 0)-cnt*B[l]-cnt*(cnt-1)/2-(r-l+1)*cnt2;
}

ll f(ll i) {
  ll nx = max(i, nxA[i]), cnt2 = max(0LL, i-nxA[i]);
  return -(nx ? ps[nx-1] : 0)+nx*B[i]-B[i] + (-nx*nx+nx)/2 + cnt2*i - cnt2;
}

ll g(ll j) {
  return ps[j] + (-j*j-j)/2;
}

ll h(ll i) {
  return nxA[i]-B[i];
}

void solve() {
  deque <Line> dq;
  dq.push_back({h(0), f(0), 0});
  for (ll i=0; i<n; ++i) {
    while (dq.size() > 1) {
      auto I = dq.front();
      dq.pop_front();
      auto J = dq.front();
      if ((ld)i-intx(I, J) > ep || fabs((ld)i-intx(I, J)) <= ep) continue;
      else {
        dq.push_front(I);
        break;
      }
    }
    auto X = dq.front();
    dp[i] = X.m * i + X.c + g(i) + mid;
    tot[i] = X.x+1;
    if (i != n-1) {
      Line K = {h(i+1), dp[i] + f(i+1), tot[i]};
      while (dq.size() > 1) {
        auto J = dq.back();
        dq.pop_back();
        auto I = dq.back();
        if (intx(I, J)-intx(I, K) > ep || fabs(intx(I, J)-intx(I, K)) <= ep) continue;
        else {
          dq.push_back(J);
          break;
        }
      }
      dq.push_back(K);
    }
  }
}


int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cin >> n >> k >> S;
  for (int i=0; i<2*n; ++i) {
    if (S[i] == 'A') {
      ps.push_back((ps.empty() ? 0 : ps.back()) + i);
      A.push_back(i);
    }
    else {
      B.push_back(i);
      nxA.push_back(A.size());
    }
  }
  l = 0, r = n*n;
  while (l < r) {
    mid = (l+r+1)/2;
    solve();
    if (k > tot[n-1]) r = mid-1;
    else l = mid;
  }
  mid = l;
  solve();
  cout << dp[n-1] - k * mid << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 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 348 KB Output is correct
11 Correct 0 ms 2396 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 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 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 348 KB Output is correct
11 Correct 0 ms 2396 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 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 468 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 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 348 KB Output is correct
11 Correct 0 ms 2396 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 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 468 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 4 ms 860 KB Output is correct
30 Correct 5 ms 2732 KB Output is correct
31 Correct 6 ms 856 KB Output is correct
32 Correct 4 ms 2652 KB Output is correct
33 Correct 4 ms 2652 KB Output is correct
34 Correct 4 ms 2652 KB Output is correct
35 Correct 4 ms 860 KB Output is correct
36 Correct 6 ms 856 KB Output is correct
37 Correct 5 ms 736 KB Output is correct
38 Correct 3 ms 860 KB Output is correct
39 Correct 5 ms 860 KB Output is correct
40 Correct 4 ms 860 KB Output is correct
41 Correct 4 ms 2732 KB Output is correct
42 Correct 4 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 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 348 KB Output is correct
11 Correct 0 ms 2396 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 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 468 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 4 ms 860 KB Output is correct
30 Correct 5 ms 2732 KB Output is correct
31 Correct 6 ms 856 KB Output is correct
32 Correct 4 ms 2652 KB Output is correct
33 Correct 4 ms 2652 KB Output is correct
34 Correct 4 ms 2652 KB Output is correct
35 Correct 4 ms 860 KB Output is correct
36 Correct 6 ms 856 KB Output is correct
37 Correct 5 ms 736 KB Output is correct
38 Correct 3 ms 860 KB Output is correct
39 Correct 5 ms 860 KB Output is correct
40 Correct 4 ms 860 KB Output is correct
41 Correct 4 ms 2732 KB Output is correct
42 Correct 4 ms 2652 KB Output is correct
43 Correct 77 ms 4036 KB Output is correct
44 Correct 141 ms 6464 KB Output is correct
45 Correct 138 ms 8000 KB Output is correct
46 Correct 85 ms 7808 KB Output is correct
47 Correct 82 ms 8348 KB Output is correct
48 Correct 88 ms 11392 KB Output is correct
49 Correct 92 ms 11392 KB Output is correct
50 Correct 133 ms 11072 KB Output is correct
51 Correct 148 ms 11072 KB Output is correct
52 Correct 97 ms 11324 KB Output is correct
53 Correct 94 ms 11328 KB Output is correct
54 Correct 141 ms 11072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 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 348 KB Output is correct
11 Correct 0 ms 2396 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 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 468 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 4 ms 860 KB Output is correct
30 Correct 5 ms 2732 KB Output is correct
31 Correct 6 ms 856 KB Output is correct
32 Correct 4 ms 2652 KB Output is correct
33 Correct 4 ms 2652 KB Output is correct
34 Correct 4 ms 2652 KB Output is correct
35 Correct 4 ms 860 KB Output is correct
36 Correct 6 ms 856 KB Output is correct
37 Correct 5 ms 736 KB Output is correct
38 Correct 3 ms 860 KB Output is correct
39 Correct 5 ms 860 KB Output is correct
40 Correct 4 ms 860 KB Output is correct
41 Correct 4 ms 2732 KB Output is correct
42 Correct 4 ms 2652 KB Output is correct
43 Correct 77 ms 4036 KB Output is correct
44 Correct 141 ms 6464 KB Output is correct
45 Correct 138 ms 8000 KB Output is correct
46 Correct 85 ms 7808 KB Output is correct
47 Correct 82 ms 8348 KB Output is correct
48 Correct 88 ms 11392 KB Output is correct
49 Correct 92 ms 11392 KB Output is correct
50 Correct 133 ms 11072 KB Output is correct
51 Correct 148 ms 11072 KB Output is correct
52 Correct 97 ms 11324 KB Output is correct
53 Correct 94 ms 11328 KB Output is correct
54 Correct 141 ms 11072 KB Output is correct
55 Correct 1034 ms 39016 KB Output is correct
56 Correct 1399 ms 61468 KB Output is correct
57 Correct 1670 ms 60440 KB Output is correct
58 Correct 1014 ms 58636 KB Output is correct
59 Correct 1042 ms 63960 KB Output is correct
60 Correct 1058 ms 63912 KB Output is correct
61 Correct 1027 ms 63624 KB Output is correct
62 Correct 1611 ms 60460 KB Output is correct
63 Correct 1696 ms 60452 KB Output is correct
64 Correct 1052 ms 63492 KB Output is correct
65 Correct 1029 ms 63292 KB Output is correct
66 Correct 1760 ms 60792 KB Output is correct
67 Correct 1054 ms 63260 KB Output is correct