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>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"
const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// -------------------------------------------------- Main Code --------------------------------------------------
const int N = 2e5+5;
int arr[N];
vi pos[N];
void sol() {
int n, k; cin >> n >> k;
for (int i = 1; i <= n; i++) {
char x; cin >> x;
if (x == 'J') arr[i] = 0;
else if (x == 'O') arr[i] = 1;
else arr[i] = 2;
pos[arr[i]].pb(i);
}
auto get = [&](int ch, int idx) {
if (idx == -1) return -1;
int i = lower_bound(all(pos[ch]), idx) - pos[ch].begin();
i += k-1;
if (i >= pos[ch].size()) return -1;
return pos[ch][i]+1;
};
int ans = inf;
for (int i = 1; i <= n; i++) {
int j = i;
j = get(0, j);
j = get(1, j);
j = get(2, j);
if (j == -1) break;
int x = i-1 + n-j+1;
ans = min(ans, n - x - k*3);
}
if (ans == inf) cout << -1 << endl;
else cout << ans << endl;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
sol();
}
return 0;
}
Compilation message (stderr)
ho_t2.cpp: In lambda function:
ho_t2.cpp:53:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | if (i >= pos[ch].size()) return -1;
| ~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |