Submission #882552

#TimeUsernameProblemLanguageResultExecution timeMemory
882552MilosMilutinovicChorus (JOI23_chorus)C++14
40 / 100
7080 ms604 KiB
#include <bits/stdc++.h>

using namespace std;

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<int> 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]];
  }
  auto Solve = [&](int L, int R) {
    int res = 0;
    for (int i = L; i <= R; i++) {
      res += max(0, ia[a[R]] - ib[b[i]]);
    }
    return res;
  };
  for (int i = 0; i < n; i++) {
    if (i > 0) {
      pref[i] = pref[i - 1];
    } else {
      pref[i] = 0;
    }
    pref[i] += ib[b[i]];
  }
  auto GetSum = [&](int L, int R) {
    return pref[R] - (L == 0 ? 0 : pref[L - 1]);
  };
  const int inf = (int) 1e9;
  vector<int> dp(n + 1, inf);
  dp[0] = 0;
  for (int foo = 0; foo < k; foo++) {
    vector<int> new_dp(n + 1, inf);
    int ptr = -1;
    for (int i = 0; i < n; i++) {
      while (ptr + 1 <= i && ia[a[i]] >= ib[b[ptr + 1]]) {
        ptr += 1;
      }
      for (int j = ptr + 1; j <= i; j++) {
        new_dp[i + 1] = min(new_dp[i + 1], dp[j]);
      }
      for (int j = 0; j <= ptr; j++) {
        new_dp[i + 1] = min(new_dp[i + 1], dp[j] + ia[a[i]] * (ptr - j + 1) - GetSum(j, ptr));
      }
    }
    swap(dp, new_dp);
  }
  cout << dp[n] << '\n';
  return 0;
}

Compilation message (stderr)

chorus.cpp: In function 'int main()':
chorus.cpp:35:8: warning: variable 'Solve' set but not used [-Wunused-but-set-variable]
   35 |   auto Solve = [&](int L, int R) {
      |        ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...