#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;
int ostatnij=n+2, ostatnio=n+2;
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 = 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;
if (kolejnal[ostatnij]==n+1) kolejnal[ostatnij] = i;
ostatnio = i;
}
else{
indeksyi.push(i);
if ((int)indeksyi.size()==k){
kolejnak[indeksyi.front()] = i;
indeksyi.pop();
}
if (kolejnal[ostatnio] == n+1) kolejnal[ostatnio] = i;
}
}
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 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |