Submission #443560

#TimeUsernameProblemLanguageResultExecution timeMemory
443560MilosMilutinovicJJOOII 2 (JOI20_ho_t2)C++14
0 / 100
1 ms312 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> pref(n);
  for (int i = 0; i < n; i++) {
    if (i > 0) {
      pref[i] = pref[i - 1];
    }
    if (s[i] == 'O') {
      pref[i]++;
    }
  }
  vector<int> x, y;
  for (int i = 0; i < n; i++) {
  	if (s[i] == 'J') {
      x.push_back(i);
    }
    if (s[i] == 'I') {
      y.push_back(i);
    }
  }
  int ans = n + 1;
  for (int i = k - 1, j = 0; i < (int) x.size(); i++) {
    while (j < (int) y.size() && pref[y[j]] - pref[x[i]] < k) {
      j++;
    }
    if (j <= (int) y.size() - k && pref[y[j]] - pref[x[i]] >= k) {
      ans = min(ans, n - 3 * k - x[i] - (n - y[j] - 1));  
    }
  }
  cout << (ans > n ? -1 : ans);
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...