#include <bits/stdc++.h>
using namespace std;
const int MAX=2*1e5+7;
int kolejnak[MAX];
int kolejnal[MAX];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
string s;
cin >> s;
queue<int> indeksyj;
queue<int> indeksyo;
queue<int> indeksyi;
queue<int> ostatnij;
queue<int> ostatnio;
for (int i=1; i<=n; i++){
kolejnak[i] = n+1;
kolejnal[i] = n+1;
}
kolejnak[n+1] = n+1;
kolejnal[n+1] = n+1;
for (int i=1; i<=n; i++){
//cout << i << " " << kolejnal[2] << " " << ostatnij << endl;
if (s[i-1]=='J'){
indeksyj.push(i);
if ((int)indeksyj.size()==k){
kolejnak[indeksyj.front()] = i;
indeksyj.pop();
}
ostatnij.push(i);
}
else if (s[i-1]=='O'){
indeksyo.push(i);
if ((int)indeksyo.size()==k){
kolejnak[indeksyo.front()] = i;
indeksyo.pop();
}
//cout << ostatnij << " " << i << endl;
while (!ostatnij.empty()){
kolejnal[ostatnij.front()] = i;
ostatnij.pop();
}
ostatnio.push(i);
}
else{
indeksyi.push(i);
if ((int)indeksyi.size()==k){
kolejnak[indeksyi.front()] = i;
indeksyi.pop();
}
while (!ostatnio.empty()){
kolejnal[ostatnio.front()] = i;
ostatnio.pop();
}
}
}
int wyn=1e9, gdzieskoncze;
for (int i=1; i<=n; i++){
//cout << kolejnak[i] << " " << kolejnal[i] << " " << i << endl;
if (s[i-1]=='J'){
gdzieskoncze = kolejnak[i];
gdzieskoncze = kolejnal[gdzieskoncze];
//cout << i << " " << gdzieskoncze << endl;
gdzieskoncze = kolejnak[gdzieskoncze];
gdzieskoncze = kolejnal[gdzieskoncze];
gdzieskoncze = kolejnak[gdzieskoncze];
//cout << gdzieskoncze << endl;
if (gdzieskoncze==n+1) continue;
wyn = min(wyn, (gdzieskoncze-i-3*k+1));
}
}
if (wyn==1e9) cout << "-1\n";
else cout << wyn << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
508 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
508 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
504 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
508 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
504 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
504 KB |
Output is correct |
36 |
Correct |
6 ms |
2012 KB |
Output is correct |
37 |
Correct |
5 ms |
2268 KB |
Output is correct |
38 |
Correct |
6 ms |
2436 KB |
Output is correct |
39 |
Correct |
5 ms |
2268 KB |
Output is correct |
40 |
Correct |
5 ms |
2268 KB |
Output is correct |
41 |
Correct |
5 ms |
2268 KB |
Output is correct |
42 |
Correct |
7 ms |
2268 KB |
Output is correct |
43 |
Correct |
3 ms |
1616 KB |
Output is correct |
44 |
Correct |
4 ms |
2012 KB |
Output is correct |
45 |
Correct |
6 ms |
2268 KB |
Output is correct |
46 |
Correct |
5 ms |
2328 KB |
Output is correct |
47 |
Correct |
5 ms |
2500 KB |
Output is correct |
48 |
Correct |
5 ms |
2328 KB |
Output is correct |
49 |
Correct |
4 ms |
1756 KB |
Output is correct |
50 |
Correct |
5 ms |
2432 KB |
Output is correct |
51 |
Correct |
5 ms |
2328 KB |
Output is correct |
52 |
Correct |
7 ms |
2804 KB |
Output is correct |
53 |
Correct |
5 ms |
2796 KB |
Output is correct |
54 |
Correct |
4 ms |
2268 KB |
Output is correct |
55 |
Correct |
4 ms |
2780 KB |
Output is correct |
56 |
Correct |
3 ms |
3036 KB |
Output is correct |