Submission #1051965

#TimeUsernameProblemLanguageResultExecution timeMemory
1051965fryingducJJOOII 2 (JOI20_ho_t2)C++17
100 / 100
6 ms3924 KiB
#include "bits/stdc++.h" using namespace std; const int maxn = 2e5 + 5; int n, k, a[maxn], pref[maxn], suf[maxn]; int ps[maxn]; string s; void solve() { cin >> n >> k >> s; s = ' ' + s; vector<int> lst; for(int i = 1; i <= n; ++i) { ps[i] = ps[i - 1] + (s[i] == 'O'); if(s[i] != 'J') continue; lst.push_back(i); pref[i] = -1; if((int)lst.size() < k) continue; int pos = lst[(int)lst.size() - k]; pref[i] = (i - pos - k + 1); } lst.clear(); memset(suf, 0x3f, sizeof(suf)); for(int i = n; i; --i) { if(s[i] == 'I') { lst.push_back(i); if((int)lst.size() >= k) { int pos = lst[(int)lst.size() - k]; suf[i] = (pos - k + 1); } } } for(int i = n - 1; i; --i) { suf[i] = min(suf[i], suf[i + 1]); } int ans = 1e9; for(int i = 1; i <= n; ++i) { if(s[i] != 'J' || pref[i] < 0) continue; int x = lower_bound(ps + 1, ps + n + 1, ps[i] + k) - ps; if(x > n) continue; ans = min(ans, (x - i - k) + pref[i] + suf[x + 1] - x - 1); } cout << (ans == 1e9 ? -1 : ans); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...