#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long int
const ll N = 100+5 , INF = 1e18 , MOD = 1e9+7;
void solve(){
ll n,k;
string s;
cin >> n >> k >> s;
s = ' '+s;
vector<ll> nearj(n+5) , nearo(n+5),neari(n+5);
ll lastj = 0 , lasto = 0 , lasti = 0;
for(ll i = 1; i <= n; i++){
nearj[i] = lastj;
nearo[i] = lasto;
neari[i] = lasti;
if(s[i] == 'J') lastj = i;
else if(s[i] == 'O') lasto = i;
else lasti = i;
}
vector<vector<ll>> par(n+5,vector<ll>(log2(k)+5));
for(ll i = 1; i <= n; i++){
if(s[i] == 'J') par[i][0] = nearj[i];
else if(s[i] == 'O') par[i][0] = nearo[i];
else par[i][0] = neari[i];
}
for(ll j = 1; j <= log2(k); j++){
for(ll i = 1; i <= n; i++){
par[i][j] = par[par[i][j-1]][j-1];
}
}
ll ans = INF;
ll jump = k-1;
for(ll i = 1; i <= n; i++){
if(s[i] == 'I'){
ll x = i;
for(ll j = 0; j <= log2(k); j++){
if(jump&(1ll<<j)){
x = par[x][j];
}
}
x = nearo[x];
for(ll j = 0; j <= log2(k); j++){
if(jump&(1ll<<j)){
x = par[x][j];
}
}
x = nearj[x];
for(ll j = 0; j <= log2(k); j++){
if(jump&(1ll<<j)){
x = par[x][j];
}
}
if(x > 0) ans = min(ans,(i-x+1)-(3*k));
}
}
if(ans == INF) ans = -1;
cout << ans;
}
int main(){
fast;
ll tc = 1;
// cin >> tc;
while(tc--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
724 KB |
Output is correct |
35 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
724 KB |
Output is correct |
35 |
Correct |
1 ms |
724 KB |
Output is correct |
36 |
Correct |
18 ms |
17924 KB |
Output is correct |
37 |
Correct |
19 ms |
19228 KB |
Output is correct |
38 |
Correct |
19 ms |
19316 KB |
Output is correct |
39 |
Correct |
96 ms |
37652 KB |
Output is correct |
40 |
Correct |
113 ms |
41116 KB |
Output is correct |
41 |
Correct |
111 ms |
41376 KB |
Output is correct |
42 |
Correct |
75 ms |
35156 KB |
Output is correct |
43 |
Correct |
71 ms |
25812 KB |
Output is correct |
44 |
Correct |
89 ms |
32144 KB |
Output is correct |
45 |
Correct |
113 ms |
41372 KB |
Output is correct |
46 |
Correct |
115 ms |
41516 KB |
Output is correct |
47 |
Correct |
115 ms |
41412 KB |
Output is correct |
48 |
Correct |
115 ms |
41448 KB |
Output is correct |
49 |
Correct |
72 ms |
27612 KB |
Output is correct |
50 |
Correct |
113 ms |
41380 KB |
Output is correct |
51 |
Correct |
124 ms |
41492 KB |
Output is correct |
52 |
Correct |
107 ms |
42860 KB |
Output is correct |
53 |
Correct |
115 ms |
44544 KB |
Output is correct |
54 |
Correct |
18 ms |
19484 KB |
Output is correct |
55 |
Correct |
97 ms |
44508 KB |
Output is correct |
56 |
Correct |
104 ms |
44548 KB |
Output is correct |