#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+5;
int pref[3][MAXN],dp[3][MAXN];
int main(){
int N,K; cin >> N >> K;
for(int i = 1;i<=N;++i){
char c; cin >> c;
if(c == 'J') pref[0][i]++;
else if(c == 'O') pref[1][i]++;
else pref[2][i]++;
pref[0][i] += pref[0][i-1];
pref[1][i] += pref[1][i-1];
pref[2][i] += pref[2][i-1];
}
memset(dp[0],0x3f,sizeof(dp[0]));
int cur = 1e9, pos = 0;
for(int i = 1;i<=N;++i){
while(pref[0][i]-pref[0][pos] >= K && pos < i){
cur = min(cur,-pos);
pos++;
}
dp[0][i] = cur+i;
}
memset(dp[1],0x3f,sizeof(dp[1]));
cur = 1e9; pos = 0;
for(int i = 1;i<=N;++i){
while(pref[1][i]-pref[1][pos] >= K && pos < i){
cur = min(cur,dp[0][pos]-pos);
pos++;
}
dp[1][i] = cur+i;
}
memset(dp[2],0x3f,sizeof(dp[2]));
cur = 1e9; pos = 0;
for(int i = 1;i<=N;++i){
while(pref[2][i]-pref[2][pos] >= K && pos < i){
cur = min(cur,dp[1][pos]-pos);
pos++;
}
dp[2][i] = cur+i;
}
int ans = *min_element(dp[2],dp[2]+N+1) - 3*K;
if(ans > 1e7) cout << -1 << "\n";
else cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2608 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2608 KB |
Output is correct |
8 |
Correct |
1 ms |
2612 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2584 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2608 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2608 KB |
Output is correct |
8 |
Correct |
1 ms |
2612 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2584 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2608 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
1 ms |
2612 KB |
Output is correct |
19 |
Correct |
1 ms |
2644 KB |
Output is correct |
20 |
Correct |
1 ms |
2644 KB |
Output is correct |
21 |
Correct |
1 ms |
2644 KB |
Output is correct |
22 |
Correct |
1 ms |
2644 KB |
Output is correct |
23 |
Correct |
1 ms |
2644 KB |
Output is correct |
24 |
Correct |
1 ms |
2612 KB |
Output is correct |
25 |
Correct |
1 ms |
2644 KB |
Output is correct |
26 |
Correct |
1 ms |
2648 KB |
Output is correct |
27 |
Correct |
1 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
1 ms |
2644 KB |
Output is correct |
30 |
Correct |
1 ms |
2644 KB |
Output is correct |
31 |
Correct |
1 ms |
2644 KB |
Output is correct |
32 |
Correct |
1 ms |
2644 KB |
Output is correct |
33 |
Correct |
1 ms |
2644 KB |
Output is correct |
34 |
Correct |
1 ms |
2644 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2608 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2608 KB |
Output is correct |
8 |
Correct |
1 ms |
2612 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2584 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2608 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
1 ms |
2612 KB |
Output is correct |
19 |
Correct |
1 ms |
2644 KB |
Output is correct |
20 |
Correct |
1 ms |
2644 KB |
Output is correct |
21 |
Correct |
1 ms |
2644 KB |
Output is correct |
22 |
Correct |
1 ms |
2644 KB |
Output is correct |
23 |
Correct |
1 ms |
2644 KB |
Output is correct |
24 |
Correct |
1 ms |
2612 KB |
Output is correct |
25 |
Correct |
1 ms |
2644 KB |
Output is correct |
26 |
Correct |
1 ms |
2648 KB |
Output is correct |
27 |
Correct |
1 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
1 ms |
2644 KB |
Output is correct |
30 |
Correct |
1 ms |
2644 KB |
Output is correct |
31 |
Correct |
1 ms |
2644 KB |
Output is correct |
32 |
Correct |
1 ms |
2644 KB |
Output is correct |
33 |
Correct |
1 ms |
2644 KB |
Output is correct |
34 |
Correct |
1 ms |
2644 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
14 ms |
5032 KB |
Output is correct |
37 |
Correct |
15 ms |
5076 KB |
Output is correct |
38 |
Correct |
16 ms |
5152 KB |
Output is correct |
39 |
Correct |
16 ms |
5076 KB |
Output is correct |
40 |
Correct |
15 ms |
5076 KB |
Output is correct |
41 |
Correct |
16 ms |
5076 KB |
Output is correct |
42 |
Correct |
17 ms |
5176 KB |
Output is correct |
43 |
Correct |
11 ms |
4148 KB |
Output is correct |
44 |
Correct |
13 ms |
4616 KB |
Output is correct |
45 |
Correct |
18 ms |
5112 KB |
Output is correct |
46 |
Correct |
14 ms |
5188 KB |
Output is correct |
47 |
Correct |
17 ms |
5100 KB |
Output is correct |
48 |
Correct |
15 ms |
5076 KB |
Output is correct |
49 |
Correct |
12 ms |
4336 KB |
Output is correct |
50 |
Correct |
15 ms |
5164 KB |
Output is correct |
51 |
Correct |
15 ms |
5148 KB |
Output is correct |
52 |
Correct |
12 ms |
5084 KB |
Output is correct |
53 |
Correct |
12 ms |
5112 KB |
Output is correct |
54 |
Correct |
10 ms |
5148 KB |
Output is correct |
55 |
Correct |
10 ms |
5076 KB |
Output is correct |
56 |
Correct |
11 ms |
5076 KB |
Output is correct |