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
using namespace std;
using pii = pair<int, int>;
void solve()
{
int n, k;
string s;
cin >> n >> k >> s;
vector<int> pj, po, pi;
for (int i = 0; i < n; ++i)
{
if (s[i] == 'J')
pj.emplace_back(i);
else if (s[i] == 'O')
po.emplace_back(i);
else
pi.emplace_back(i);
}
int ans = INT_MAX;
for (int i = n; i >= 0; --i)
{
if (i != n)
{
if (s[i] == 'J')
pj.pop_back();
else if (s[i] == 'O')
po.pop_back();
else
pi.pop_back();
}
if ((int)pi.size() < k)
continue;
int id = pi[(int)pi.size() - k];
id = lower_bound(po.begin(), po.end(), id) - po.begin();
if (id < k)
continue;
id -= k;
id = lower_bound(pj.begin(), pj.end(), po[id]) - pj.begin();
if (id < k)
continue;
id -= k;
ans = min(ans, i - pj[id]);
}
if (ans == INT_MAX)
{
cout << "-1\n";
return;
}
cout << ans - k * 3 << '\n';
}
/*
10 2
OJIJOIOIIJ
9 3
JJJOOOIII
9 1
IIIOOOJJJ
*/
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |