제출 #1129663

#제출 시각아이디문제언어결과실행 시간메모리
1129663am_aadvikJJOOII 2 (JOI20_ho_t2)C++17
100 / 100
32 ms3876 KiB
#include <iostream> #include <vector> #include<math.h> using namespace std; int main() { int n, k; cin >> n >> k; string s, l = "JOI"; cin >> s; vector<vector<int>> pre(3, vector<int>(n, 0)); for (int i = 0; i < 3; ++i) { int x = n - 1, y = n - 1, cnt = 0, cur = -1; while (((x >= 0) && (y >= 0))) { while ((x >= 0) && (cnt < k)) { if (s[x] == l[i]) cnt++; if (cnt == k) break; pre[i][x] = cur; --x; } if (x < 0) break; for (; y >= x; --y) if (s[y] == l[i]) break; cur = y, y--, cnt--, pre[i][x--] = cur; } } int ans = n + 1; for (int f = 0; f < n; ++f) { int l = f + 2, r = n - 1; while (l <= r) { int e = (l + r) / 2, j = f; bool ok = 1; for (int i = 0; i < 3; ++i) { j = pre[i][j]; if ((j == -1) || (j > e)) { ok = 0; break; } } if (ok) ans = min(ans, e - f + 1), r = e - 1; else l = e + 1; } } cout << ((ans == (n + 1)) ? -1 : ans - (3 * k)) << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...