#include <bits/stdc++.h>
using namespace std;
const long long INFll = (long long) 1e18 + 10;
const int INFii = (int) 1e9 + 10;
typedef long long ll;
typedef int ii;
typedef long double dbl;
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define maxn 220000
ii n, k, a[maxn], dp[maxn][5], pos[5][maxn], px[maxn], qtd[5];
string s;
ii sol(ii i, ii p) {
if(p == 3) return 0;
if(i == n+1) return INFii;
if(dp[i][p] != -1) return dp[i][p];
ii ans = INFii;
ans = min(ans, sol(i+1,p)+1);
if(p == 0) ans = min(ans, sol(i+1,p));
if(a[i] == p && px[i]+k-1 <= qtd[p]) {
ans = min(ans, sol(pos[p][px[i]+k-1] + 1 ,p+1) + pos[p][px[i]+k-1]-i+1-k);
}
return dp[i][p] = ans;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
//freopen("in.in", "r", stdin);
//freopen("out.out", "w", stdout);
cin >> n >> k >> s;
for(ii i = 1; i <= n; i++) {
dp[i][0] = dp[i][1] = dp[i][2] = dp[i][3] = -1;
if(s[i-1] == 'J') {
a[i] = 0;
px[i] = ++qtd[0];
pos[0][qtd[0]] = i;
}
else if(s[i-1] == 'O') {
a[i] = 1;
px[i] = ++qtd[1];
pos[1][qtd[1]] = i;
}
else {
a[i] = 2;
px[i] = ++qtd[2];
pos[2][qtd[2]] = i;
}
}
if(sol(1,0) >= INFii) cout << -1 << endl;
else cout << sol(1,0) << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
444 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
588 KB |
Output is correct |
26 |
Correct |
1 ms |
588 KB |
Output is correct |
27 |
Correct |
1 ms |
588 KB |
Output is correct |
28 |
Correct |
1 ms |
588 KB |
Output is correct |
29 |
Correct |
1 ms |
588 KB |
Output is correct |
30 |
Correct |
1 ms |
588 KB |
Output is correct |
31 |
Correct |
0 ms |
444 KB |
Output is correct |
32 |
Correct |
0 ms |
588 KB |
Output is correct |
33 |
Correct |
0 ms |
588 KB |
Output is correct |
34 |
Correct |
1 ms |
588 KB |
Output is correct |
35 |
Correct |
1 ms |
576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
444 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
588 KB |
Output is correct |
26 |
Correct |
1 ms |
588 KB |
Output is correct |
27 |
Correct |
1 ms |
588 KB |
Output is correct |
28 |
Correct |
1 ms |
588 KB |
Output is correct |
29 |
Correct |
1 ms |
588 KB |
Output is correct |
30 |
Correct |
1 ms |
588 KB |
Output is correct |
31 |
Correct |
0 ms |
444 KB |
Output is correct |
32 |
Correct |
0 ms |
588 KB |
Output is correct |
33 |
Correct |
0 ms |
588 KB |
Output is correct |
34 |
Correct |
1 ms |
588 KB |
Output is correct |
35 |
Correct |
1 ms |
576 KB |
Output is correct |
36 |
Correct |
16 ms |
21208 KB |
Output is correct |
37 |
Correct |
18 ms |
22692 KB |
Output is correct |
38 |
Correct |
18 ms |
22716 KB |
Output is correct |
39 |
Correct |
18 ms |
22488 KB |
Output is correct |
40 |
Correct |
17 ms |
22744 KB |
Output is correct |
41 |
Correct |
18 ms |
22740 KB |
Output is correct |
42 |
Correct |
22 ms |
22744 KB |
Output is correct |
43 |
Correct |
10 ms |
14284 KB |
Output is correct |
44 |
Correct |
15 ms |
17624 KB |
Output is correct |
45 |
Correct |
19 ms |
22760 KB |
Output is correct |
46 |
Correct |
16 ms |
22744 KB |
Output is correct |
47 |
Correct |
17 ms |
22696 KB |
Output is correct |
48 |
Correct |
16 ms |
22776 KB |
Output is correct |
49 |
Correct |
11 ms |
15320 KB |
Output is correct |
50 |
Correct |
15 ms |
22744 KB |
Output is correct |
51 |
Correct |
16 ms |
22744 KB |
Output is correct |
52 |
Correct |
18 ms |
21976 KB |
Output is correct |
53 |
Correct |
17 ms |
22744 KB |
Output is correct |
54 |
Correct |
21 ms |
22700 KB |
Output is correct |
55 |
Correct |
16 ms |
22668 KB |
Output is correct |
56 |
Correct |
15 ms |
22680 KB |
Output is correct |