#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MN = 2e5+5;
int N, K, i, j, psa[MN][3], dp[MN][3], ans=1<<30;
string s;
int main(){
cin >> N >> K >> s;
s.insert(s.begin(),' ');
for(i=1;i<=N;i++){
for(j=0;j<3;j++)
psa[i][j]=psa[i-1][j];
if(s[i]=='J') psa[i][0]++;
else if(s[i]=='O') psa[i][1]++;
else if(s[i]=='I') psa[i][2]++;
}
memset(dp,-1,sizeof(dp));
for(int k=0;k<3;k++){
for(i=j=1;i<=N;i++){
while(j+1<=i&&psa[i][k]-psa[j][k]>=K) j++;
if(psa[i][k]-psa[j-1][k]<K) continue;
if(!k) dp[i][k]=i-j+1-K;
else if(dp[j-1][k-1]!=-1) dp[i][k]=dp[j-1][k-1]+i-j+1-K;
}
}
for(i=1;i<=N;i++){
if(dp[i][2]!=-1) ans=min(ans,dp[i][2]);
}
printf("%d\n",ans==1<<30?-1:ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
5 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
5 ms |
2688 KB |
Output is correct |
6 |
Correct |
5 ms |
2560 KB |
Output is correct |
7 |
Correct |
5 ms |
2688 KB |
Output is correct |
8 |
Correct |
5 ms |
2688 KB |
Output is correct |
9 |
Correct |
6 ms |
2816 KB |
Output is correct |
10 |
Correct |
6 ms |
2688 KB |
Output is correct |
11 |
Correct |
6 ms |
2560 KB |
Output is correct |
12 |
Correct |
6 ms |
2688 KB |
Output is correct |
13 |
Correct |
6 ms |
2688 KB |
Output is correct |
14 |
Correct |
5 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
5 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
5 ms |
2688 KB |
Output is correct |
6 |
Correct |
5 ms |
2560 KB |
Output is correct |
7 |
Correct |
5 ms |
2688 KB |
Output is correct |
8 |
Correct |
5 ms |
2688 KB |
Output is correct |
9 |
Correct |
6 ms |
2816 KB |
Output is correct |
10 |
Correct |
6 ms |
2688 KB |
Output is correct |
11 |
Correct |
6 ms |
2560 KB |
Output is correct |
12 |
Correct |
6 ms |
2688 KB |
Output is correct |
13 |
Correct |
6 ms |
2688 KB |
Output is correct |
14 |
Correct |
5 ms |
2688 KB |
Output is correct |
15 |
Correct |
6 ms |
2688 KB |
Output is correct |
16 |
Correct |
6 ms |
2688 KB |
Output is correct |
17 |
Correct |
6 ms |
2688 KB |
Output is correct |
18 |
Correct |
6 ms |
2688 KB |
Output is correct |
19 |
Correct |
6 ms |
2688 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
21 |
Correct |
6 ms |
2688 KB |
Output is correct |
22 |
Correct |
6 ms |
2688 KB |
Output is correct |
23 |
Correct |
6 ms |
2688 KB |
Output is correct |
24 |
Correct |
6 ms |
2688 KB |
Output is correct |
25 |
Correct |
6 ms |
2688 KB |
Output is correct |
26 |
Correct |
6 ms |
2688 KB |
Output is correct |
27 |
Correct |
6 ms |
2688 KB |
Output is correct |
28 |
Correct |
6 ms |
2688 KB |
Output is correct |
29 |
Correct |
6 ms |
2816 KB |
Output is correct |
30 |
Correct |
6 ms |
2688 KB |
Output is correct |
31 |
Correct |
6 ms |
2688 KB |
Output is correct |
32 |
Correct |
6 ms |
2688 KB |
Output is correct |
33 |
Correct |
6 ms |
2688 KB |
Output is correct |
34 |
Correct |
6 ms |
2688 KB |
Output is correct |
35 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
5 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
5 ms |
2688 KB |
Output is correct |
6 |
Correct |
5 ms |
2560 KB |
Output is correct |
7 |
Correct |
5 ms |
2688 KB |
Output is correct |
8 |
Correct |
5 ms |
2688 KB |
Output is correct |
9 |
Correct |
6 ms |
2816 KB |
Output is correct |
10 |
Correct |
6 ms |
2688 KB |
Output is correct |
11 |
Correct |
6 ms |
2560 KB |
Output is correct |
12 |
Correct |
6 ms |
2688 KB |
Output is correct |
13 |
Correct |
6 ms |
2688 KB |
Output is correct |
14 |
Correct |
5 ms |
2688 KB |
Output is correct |
15 |
Correct |
6 ms |
2688 KB |
Output is correct |
16 |
Correct |
6 ms |
2688 KB |
Output is correct |
17 |
Correct |
6 ms |
2688 KB |
Output is correct |
18 |
Correct |
6 ms |
2688 KB |
Output is correct |
19 |
Correct |
6 ms |
2688 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
21 |
Correct |
6 ms |
2688 KB |
Output is correct |
22 |
Correct |
6 ms |
2688 KB |
Output is correct |
23 |
Correct |
6 ms |
2688 KB |
Output is correct |
24 |
Correct |
6 ms |
2688 KB |
Output is correct |
25 |
Correct |
6 ms |
2688 KB |
Output is correct |
26 |
Correct |
6 ms |
2688 KB |
Output is correct |
27 |
Correct |
6 ms |
2688 KB |
Output is correct |
28 |
Correct |
6 ms |
2688 KB |
Output is correct |
29 |
Correct |
6 ms |
2816 KB |
Output is correct |
30 |
Correct |
6 ms |
2688 KB |
Output is correct |
31 |
Correct |
6 ms |
2688 KB |
Output is correct |
32 |
Correct |
6 ms |
2688 KB |
Output is correct |
33 |
Correct |
6 ms |
2688 KB |
Output is correct |
34 |
Correct |
6 ms |
2688 KB |
Output is correct |
35 |
Correct |
6 ms |
2688 KB |
Output is correct |
36 |
Correct |
22 ms |
5520 KB |
Output is correct |
37 |
Correct |
23 ms |
5520 KB |
Output is correct |
38 |
Correct |
23 ms |
5520 KB |
Output is correct |
39 |
Correct |
25 ms |
5520 KB |
Output is correct |
40 |
Correct |
23 ms |
5520 KB |
Output is correct |
41 |
Correct |
24 ms |
5520 KB |
Output is correct |
42 |
Correct |
25 ms |
5520 KB |
Output is correct |
43 |
Correct |
16 ms |
4496 KB |
Output is correct |
44 |
Correct |
19 ms |
4880 KB |
Output is correct |
45 |
Correct |
23 ms |
5520 KB |
Output is correct |
46 |
Correct |
23 ms |
5520 KB |
Output is correct |
47 |
Correct |
23 ms |
5520 KB |
Output is correct |
48 |
Correct |
23 ms |
5520 KB |
Output is correct |
49 |
Correct |
18 ms |
4624 KB |
Output is correct |
50 |
Correct |
23 ms |
5520 KB |
Output is correct |
51 |
Correct |
25 ms |
5812 KB |
Output is correct |
52 |
Correct |
20 ms |
5520 KB |
Output is correct |
53 |
Correct |
20 ms |
5520 KB |
Output is correct |
54 |
Correct |
19 ms |
5520 KB |
Output is correct |
55 |
Correct |
18 ms |
5520 KB |
Output is correct |
56 |
Correct |
18 ms |
5520 KB |
Output is correct |