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 pb push_back
using namespace std;
typedef long long ll;
const int N = 5e5;
const int INF = 1e9;
string s;
int n, l;
vector<int> pos[3];
int nxt[3][N], cnt[3];
int get(int i, int res = 0) {
int nxt_i = nxt[0][i + l];
if (nxt_i == -1) return INF;
res += nxt_i - pos[0][i] + 1 - l;
int j = upper_bound(pos[1].begin(), pos[1].end(), nxt_i) - pos[1].begin();
if (j == pos[1].size()) return INF;
res += pos[1][j] - nxt_i - 1;
int nxt_j = nxt[1][j + l];
if (nxt_j == -1) return INF;
res += nxt_j - pos[1][j] + 1 - l;
int k = upper_bound(pos[2].begin(), pos[2].end(), nxt_j) - pos[2].begin();
if (k == pos[2].size()) return INF;
res += pos[2][k] - nxt_j - 1;
int nxt_k = nxt[2][k + l];
if (nxt_k == -1) return INF;
res += nxt_k - pos[2][k] + 1 - l;
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
for (int i = 0; i < 3; i++) fill(nxt[i], nxt[i] + N, -1);
cin >> n >> l;
cin >> s;
for (int i = 0; i < n; i++) {
char c = s[i];
if (c == 'J') {
pos[0].pb(i);
nxt[0][++cnt[0]] = i;
}
else if (c == 'O') {
pos[1].pb(i);
nxt[1][++cnt[1]] = i;
}
else {
pos[2].pb(i);
nxt[2][++cnt[2]] = i;
}
}
int ans = INF;
for (int i = 0; i < pos[0].size(); i++) {
ans = min(ans, get(i));
}
cout << (ans == INF ? -1 : ans);
return 0;
}
Compilation message (stderr)
ho_t2.cpp: In function 'int get(int, int)':
ho_t2.cpp:22:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | if (j == pos[1].size()) return INF;
| ~~^~~~~~~~~~~~~~~~
ho_t2.cpp:28:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | if (k == pos[2].size()) return INF;
| ~~^~~~~~~~~~~~~~~~
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:60:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int i = 0; i < pos[0].size(); i++) {
| ~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |