Submission #787772

#TimeUsernameProblemLanguageResultExecution timeMemory
787772tvladm2009JJOOII 2 (JOI20_ho_t2)C++17
100 / 100
29 ms5524 KiB
#include <bits/stdc++.h> using namespace std; const int N = (int) 2e5 + 7; const int INF = (int) 1e9; int J[N], O[N], I[N]; int whereJ[N], whereO[N], whereI[N]; int n, k; string s; signed main() { ios::sync_with_stdio(0); cin.tie(0); // freopen("input", "r", stdin); cin >> n >> k >> s; s = "#" + s; for (int i = 1; i <= n; i++) { J[i] = J[i - 1]; O[i] = O[i - 1]; I[i] = I[i - 1]; if (s[i] == 'J') { J[i]++; } else if (s[i] == 'O') { O[i]++; } else { I[i]++; } } for (int i = 1; i <= n; i++) { int low = 1, high = n; while (low <= high) { int mid = (low + high) / 2; if (J[mid] - J[i - 1] >= k) { whereJ[i] = mid; high = mid - 1; } else { low = mid + 1; } } } for (int i = 1; i <= n; i++) { int low = 1, high = n; while (low <= high) { int mid = (low + high) / 2; if (O[mid] - O[i - 1] >= k) { whereO[i] = mid; high = mid - 1; } else { low = mid + 1; } } } for (int i = 1; i <= n; i++) { int low = 1, high = n; while (low <= high) { int mid = (low + high) / 2; if (I[mid] - I[i - 1] >= k) { whereI[i] = mid; high = mid - 1; } else { low = mid + 1; } } } int ret = INF; for (int i = 1; i <= n; i++) { int j = whereJ[i]; j = whereO[j]; j = whereI[j]; if (j != 0) { ret = min(ret, j - i + 1 - 3 * k); } } if (ret == INF) { ret = -1; } cout << ret << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...