This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define IO ios::sync_with_stdio(0), cin.tie(0)
#define FOR(i, a, b) for (int i = a, I = b; i <= I; i++)
using namespace std;
using pii = pair<int, int>;
void dbg() {;}
template<class T, class ...U>
void dbg(T a, U ...b) { cout << a << " "; dbg(b...); }
void ent() { cout << "\n"; }
const int INF = 1e16;
const int N = 200005;
char a[N];
int cstj[N], csti[N];
int preo[N];
signed main() {
IO;
int n, k;
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
fill(cstj, cstj + N, -1);
fill(csti, csti + N, -1);
deque<int> dq;
for (int i = 1; i <= n; i++) {
if (a[i] == 'J') {
dq.push_back(i);
while (dq.size() > k)
dq.pop_front();
if (dq.size() >= k)
cstj[i] = (i - dq.front() + 1) - k;
}
}
while (!dq.empty()) dq.pop_back();
for (int i = n; i >= 1; i--) {
if (a[i] == 'I') {
dq.push_back(i);
while (dq.size() > k)
dq.pop_front();
if (dq.size() >= k)
csti[i] = (dq.front() - i + 1) - k;
}
}
for (int i = 1; i <= n; i++)
preo[i] = preo[i - 1] + (a[i] == 'O');
int ans = INF;
int r = 1;
/// J O I
/// 1 2 3
for (int l = 1; l <= n - 2 * k; l++) {
if (cstj[l] != -1) {
r = max(r, l + 1);
while (r <= n && (csti[r] == -1 || preo[r - 1] - preo[l] < k))
r += 1;
if (r <= n && csti[r] != -1 && preo[r - 1] - preo[l] >= k) {
ans = min(ans, cstj[l] + csti[r] + (r - l - 1) - k);
}
// dbg(l, r); ent();
}
}
cout << (ans == INF ? -1ll : ans) << "\n";
return 0;
}
Compilation message (stderr)
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:31:30: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
31 | while (dq.size() > k)
| ~~~~~~~~~~^~~
ho_t2.cpp:33:27: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
33 | if (dq.size() >= k)
| ~~~~~~~~~~^~~~
ho_t2.cpp:41:30: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
41 | while (dq.size() > k)
| ~~~~~~~~~~^~~
ho_t2.cpp:43:27: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
43 | if (dq.size() >= k)
| ~~~~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |