#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define vi vector<int>
#define vs vector<string>
#define vb vector<bool>
#define vpi vector<pi>
#define pb push_back
#define all(a) (a).begin(), (a).end()
const int mod = 1e9 + 7;
int get_idx(int x, vi v)
{
if (v[0] > x)
{
return 0;
}
if (v[v.size() - 1] < x)
{
return mod;
}
int i = 0;
int j = v.size() - 1;
while (i + 1 < j)
{
int m = (i + j) / 2;
if (v[m] < x)
{
i = m;
}
else
{
j = m;
}
}
return j;
}
void solve()
{
int n, k;
cin >> n >> k;
string s;
cin >> s;
vi j, o, i;
for (int x = 0; x < s.size(); x++)
{
if (s[x] == 'J')
{
j.pb(x);
}
if (s[x] == 'O')
{
o.pb(x);
}
if (s[x] == 'I')
{
i.pb(x);
}
}
int ans = mod;
for (int x = 0; x < j.size(); x++)
{
if (x + (k - 1) >= j.size())
break;
int x1 = j[x];
int x2 = j[x + (k - 1)];
if (o[o.size() - 1] < x2)
break;
int b = get_idx(x2, o);
if (b + (k - 1) >= o.size())
break;
int y1 = o[b];
int y2 = o[b + (k - 1)];
if (i[i.size() - 1] < y2)
break;
int d = get_idx(y2, i);
if (d + (k - 1) >= i.size())
break;
int z1 = i[d];
int z2 = i[d + (k - 1)];
ans = min(ans, abs((x2 - x1) + 1) + abs((y2 - y1) + 1) + abs((z2 - z1) + 1) - ((3 * k) + ((y1 - x2) - 1) + ((z1 - y2) - 1)));
}
cout << (ans == mod ? -1 : ans) << '\n';
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin >> t;
while (t--)
{
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... |