#include <bits/stdc++.h>
#define int long long
using namespace std;
// costJ(i), costI(i)
// sol = min_{i, j} (j-i-1-K) + costJ[i] + costI[j]
signed main(void)
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int N, K;
string s;
cin >> N >> K >> s;
vector<int> costJ(N, 1e18), costI(N, 1e18);
vector<int> nbOcc(N);
for (int i(0); i < N; ++i)
{
if (i) nbOcc[i] += nbOcc[i-1];
nbOcc[i] += s[i] == 'O';
}
queue<int> q;
for (int i(0); i < N; ++i)
{
if (s[i] == 'J')
q.push(i);
while ((int)q.size() > K) q.pop();
if ((int)q.size() == K)
{
int lst = q.front();
costJ[i] = i - lst +1 - K;
}
}
while (!q.empty()) q.pop();
for (int i(N-1); i >= 0; --i)
{
if (s[i] == 'I')
q.push(i);
while ((int)q.size() > K) q.pop();
if ((int)q.size() == K)
{
int lst = q.front();
costI[i] = lst - i + 1 - K;
}
}
int sol(1e18);
for (int deb(0); deb < N; ++deb)
for (int fin(deb+1); fin < N; ++fin)
if (nbOcc[fin-1] - nbOcc[deb] >= K)
sol = min(sol, fin-deb-1-K + costJ[deb] + costI[fin]);
if (sol == 1e18)
sol = -1;
cout << sol << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
5 ms |
492 KB |
Output is correct |
16 |
Correct |
6 ms |
364 KB |
Output is correct |
17 |
Correct |
6 ms |
364 KB |
Output is correct |
18 |
Correct |
3 ms |
364 KB |
Output is correct |
19 |
Correct |
6 ms |
492 KB |
Output is correct |
20 |
Correct |
7 ms |
364 KB |
Output is correct |
21 |
Correct |
6 ms |
364 KB |
Output is correct |
22 |
Correct |
6 ms |
364 KB |
Output is correct |
23 |
Correct |
2 ms |
364 KB |
Output is correct |
24 |
Correct |
9 ms |
512 KB |
Output is correct |
25 |
Correct |
7 ms |
364 KB |
Output is correct |
26 |
Correct |
8 ms |
364 KB |
Output is correct |
27 |
Correct |
8 ms |
364 KB |
Output is correct |
28 |
Correct |
7 ms |
492 KB |
Output is correct |
29 |
Correct |
7 ms |
364 KB |
Output is correct |
30 |
Correct |
7 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
364 KB |
Output is correct |
32 |
Correct |
7 ms |
364 KB |
Output is correct |
33 |
Correct |
6 ms |
364 KB |
Output is correct |
34 |
Correct |
6 ms |
364 KB |
Output is correct |
35 |
Correct |
7 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
5 ms |
492 KB |
Output is correct |
16 |
Correct |
6 ms |
364 KB |
Output is correct |
17 |
Correct |
6 ms |
364 KB |
Output is correct |
18 |
Correct |
3 ms |
364 KB |
Output is correct |
19 |
Correct |
6 ms |
492 KB |
Output is correct |
20 |
Correct |
7 ms |
364 KB |
Output is correct |
21 |
Correct |
6 ms |
364 KB |
Output is correct |
22 |
Correct |
6 ms |
364 KB |
Output is correct |
23 |
Correct |
2 ms |
364 KB |
Output is correct |
24 |
Correct |
9 ms |
512 KB |
Output is correct |
25 |
Correct |
7 ms |
364 KB |
Output is correct |
26 |
Correct |
8 ms |
364 KB |
Output is correct |
27 |
Correct |
8 ms |
364 KB |
Output is correct |
28 |
Correct |
7 ms |
492 KB |
Output is correct |
29 |
Correct |
7 ms |
364 KB |
Output is correct |
30 |
Correct |
7 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
364 KB |
Output is correct |
32 |
Correct |
7 ms |
364 KB |
Output is correct |
33 |
Correct |
6 ms |
364 KB |
Output is correct |
34 |
Correct |
6 ms |
364 KB |
Output is correct |
35 |
Correct |
7 ms |
384 KB |
Output is correct |
36 |
Execution timed out |
2080 ms |
5240 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |