답안 #882811

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
882811 2023-12-03T18:22:39 Z MilosMilutinovic Chorus (JOI23_chorus) C++17
87 / 100
7000 ms 63860 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;
  }
};

const int MAX = 5e6;

int n, ia[MAX], ib[MAX];
long long pref[MAX];
line li[MAX];
int ls[MAX], rs[MAX], tsz, root;
vector<int> a, b;

void Update(int& x, int l, int r, line ln) {
  if (!x) {
    x = ++tsz;
    li[x].init();
  }
  int mid = l + r >> 1;
  if (li[x].val(mid) > ln.val(mid) || (ln.val(mid) == li[x].val(mid) && ln.f < li[x].f)) {
    swap(li[x], ln);
  }
  if (l == r) {
    return;
  }
  if (ln.val(l) < li[x].val(l)) {
    Update(ls[x], l, mid, ln);
  } else {
    Update(rs[x], mid + 1, r, ln);
  }
}

pair<long long, int> Query(int x, int l, int r, int i) {
  if (!x) {
    return make_pair((long long) 1e18, 0);
  }
  if (l == r) {
    return make_pair(li[x].val(i), li[x].f);
  }
  int mid = l + r >> 1;
  if (i <= mid) {
    return min(Query(ls[x], l, mid, i), make_pair(li[x].val(i), li[x].f));
  } else {
    return min(Query(rs[x], mid + 1, r, i), make_pair(li[x].val(i), li[x].f));
  }
}

void clear_seg() {
  while (tsz > 0) {
    ls[tsz] = 0;
    rs[tsz] = 0;
    li[tsz].init();
    tsz -= 1;
  }
  root = 0;
}

pair<long long, int> 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;
  clear_seg();
  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(root, 0, n, 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(root, 0, n, 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];
}
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int k;
  cin >> n >> k;
  string s;
  cin >> s;
  for (int i = 0; i < 2 * n; i++) {
    if (s[i] == 'A') {
      a.push_back(i);
    } else {
      b.push_back(i);
    }
  }
  for (int i = 0; i < n; i++) {
    ia[a[i]] = i + 1;
  }
  for (int i = 0; i < 2 * n; i++) {
    if (i > 0) {
      pref[i] = pref[i - 1];
    }
    pref[i] += (s[i] == 'A' ? 1 : 0);
  }
  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]];
  }
  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 'void Update(int&, int, int, line)':
chorus.cpp:31:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   31 |   int mid = l + r >> 1;
      |             ~~^~~
chorus.cpp: In function 'std::pair<long long int, int> Query(int, int, int, int)':
chorus.cpp:52:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   52 |   int mid = l + r >> 1;
      |             ~~^~~
chorus.cpp: In function 'int main()':
chorus.cpp:136:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  136 |     long long mid = low + high >> 1;
      |                     ~~~~^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6488 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6488 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6488 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6488 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 2 ms 6492 KB Output is correct
18 Correct 5 ms 6492 KB Output is correct
19 Correct 5 ms 6488 KB Output is correct
20 Correct 4 ms 6492 KB Output is correct
21 Correct 3 ms 6492 KB Output is correct
22 Correct 4 ms 6656 KB Output is correct
23 Correct 4 ms 6492 KB Output is correct
24 Correct 5 ms 6492 KB Output is correct
25 Correct 4 ms 6660 KB Output is correct
26 Correct 4 ms 6656 KB Output is correct
27 Correct 4 ms 6492 KB Output is correct
28 Correct 5 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6488 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6488 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 2 ms 6492 KB Output is correct
18 Correct 5 ms 6492 KB Output is correct
19 Correct 5 ms 6488 KB Output is correct
20 Correct 4 ms 6492 KB Output is correct
21 Correct 3 ms 6492 KB Output is correct
22 Correct 4 ms 6656 KB Output is correct
23 Correct 4 ms 6492 KB Output is correct
24 Correct 5 ms 6492 KB Output is correct
25 Correct 4 ms 6660 KB Output is correct
26 Correct 4 ms 6656 KB Output is correct
27 Correct 4 ms 6492 KB Output is correct
28 Correct 5 ms 6492 KB Output is correct
29 Correct 46 ms 8796 KB Output is correct
30 Correct 53 ms 8796 KB Output is correct
31 Correct 54 ms 8796 KB Output is correct
32 Correct 40 ms 8792 KB Output is correct
33 Correct 34 ms 9048 KB Output is correct
34 Correct 40 ms 8972 KB Output is correct
35 Correct 40 ms 8792 KB Output is correct
36 Correct 58 ms 8952 KB Output is correct
37 Correct 51 ms 9044 KB Output is correct
38 Correct 40 ms 8964 KB Output is correct
39 Correct 50 ms 8796 KB Output is correct
40 Correct 45 ms 8796 KB Output is correct
41 Correct 45 ms 8796 KB Output is correct
42 Correct 43 ms 9108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6488 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6488 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 2 ms 6492 KB Output is correct
18 Correct 5 ms 6492 KB Output is correct
19 Correct 5 ms 6488 KB Output is correct
20 Correct 4 ms 6492 KB Output is correct
21 Correct 3 ms 6492 KB Output is correct
22 Correct 4 ms 6656 KB Output is correct
23 Correct 4 ms 6492 KB Output is correct
24 Correct 5 ms 6492 KB Output is correct
25 Correct 4 ms 6660 KB Output is correct
26 Correct 4 ms 6656 KB Output is correct
27 Correct 4 ms 6492 KB Output is correct
28 Correct 5 ms 6492 KB Output is correct
29 Correct 46 ms 8796 KB Output is correct
30 Correct 53 ms 8796 KB Output is correct
31 Correct 54 ms 8796 KB Output is correct
32 Correct 40 ms 8792 KB Output is correct
33 Correct 34 ms 9048 KB Output is correct
34 Correct 40 ms 8972 KB Output is correct
35 Correct 40 ms 8792 KB Output is correct
36 Correct 58 ms 8952 KB Output is correct
37 Correct 51 ms 9044 KB Output is correct
38 Correct 40 ms 8964 KB Output is correct
39 Correct 50 ms 8796 KB Output is correct
40 Correct 45 ms 8796 KB Output is correct
41 Correct 45 ms 8796 KB Output is correct
42 Correct 43 ms 9108 KB Output is correct
43 Correct 781 ms 13628 KB Output is correct
44 Correct 1348 ms 15644 KB Output is correct
45 Correct 1378 ms 15668 KB Output is correct
46 Correct 809 ms 13028 KB Output is correct
47 Correct 831 ms 13240 KB Output is correct
48 Correct 1081 ms 15552 KB Output is correct
49 Correct 994 ms 15524 KB Output is correct
50 Correct 1299 ms 15400 KB Output is correct
51 Correct 1330 ms 15368 KB Output is correct
52 Correct 1018 ms 17672 KB Output is correct
53 Correct 1081 ms 17740 KB Output is correct
54 Correct 1243 ms 15628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6488 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6488 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 2 ms 6492 KB Output is correct
18 Correct 5 ms 6492 KB Output is correct
19 Correct 5 ms 6488 KB Output is correct
20 Correct 4 ms 6492 KB Output is correct
21 Correct 3 ms 6492 KB Output is correct
22 Correct 4 ms 6656 KB Output is correct
23 Correct 4 ms 6492 KB Output is correct
24 Correct 5 ms 6492 KB Output is correct
25 Correct 4 ms 6660 KB Output is correct
26 Correct 4 ms 6656 KB Output is correct
27 Correct 4 ms 6492 KB Output is correct
28 Correct 5 ms 6492 KB Output is correct
29 Correct 46 ms 8796 KB Output is correct
30 Correct 53 ms 8796 KB Output is correct
31 Correct 54 ms 8796 KB Output is correct
32 Correct 40 ms 8792 KB Output is correct
33 Correct 34 ms 9048 KB Output is correct
34 Correct 40 ms 8972 KB Output is correct
35 Correct 40 ms 8792 KB Output is correct
36 Correct 58 ms 8952 KB Output is correct
37 Correct 51 ms 9044 KB Output is correct
38 Correct 40 ms 8964 KB Output is correct
39 Correct 50 ms 8796 KB Output is correct
40 Correct 45 ms 8796 KB Output is correct
41 Correct 45 ms 8796 KB Output is correct
42 Correct 43 ms 9108 KB Output is correct
43 Correct 781 ms 13628 KB Output is correct
44 Correct 1348 ms 15644 KB Output is correct
45 Correct 1378 ms 15668 KB Output is correct
46 Correct 809 ms 13028 KB Output is correct
47 Correct 831 ms 13240 KB Output is correct
48 Correct 1081 ms 15552 KB Output is correct
49 Correct 994 ms 15524 KB Output is correct
50 Correct 1299 ms 15400 KB Output is correct
51 Correct 1330 ms 15368 KB Output is correct
52 Correct 1018 ms 17672 KB Output is correct
53 Correct 1081 ms 17740 KB Output is correct
54 Correct 1243 ms 15628 KB Output is correct
55 Execution timed out 7096 ms 63860 KB Time limit exceeded
56 Halted 0 ms 0 KB -