#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define sc second
#define pb push_back
#define in insert
ll a[200001],d[200001];
int32_t main () {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll o = 1; //cin>>o;
while (o--) {
ll n, mx; cin>>n>>mx;
string s; cin>>s;
ll x = -1 , y = -1 , z = -1;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == 'J') {
a[i] = x;
x = i;
}
if (s[i] == 'O') {
a[i] = y;
y = i;
}
if (s[i] == 'I') {
a[i] = z;
z = i;
}
}
/*
for (int i = 0; i < n; i++)
cout<<a[i]<<" ";*/
for (int i = 0; i < n; i++){
if(d[i] != 0) continue;
int k = i , l = i , cnt = 1;
while (k != -1 && cnt <= mx) {
cnt++;
l = k;
k = a[k];
}
//cout<<l<<" ";
int j = i;
while (l != -1){
d[j] = l;
j = a[j];
l = a[l];
}
while (j != -1) {
d[j] = -1;
j = a[j];
}
}
/*
for (int i = 0; i < n; i++){
cout<<d[i]<<" ";
}*/
x = -1 ; y = -1 ; z = -1;
for(int i = 0; i < n; i++) {
if(s[i] =='J') {
if (d[i] == -1) break;
x = i;
for(int j = d[i] + 1; j < n; j++) {
if (s[j] == 'O') {
if (d[j] == -1) break;
y = j;
for (int k = d[j] + 1; k < n; k++) {
if (s[k] == 'I') {
if (d[k] == -1) break;
z = k;
break;
}
}
break;
}
}
break;
}
}// cout<<x<<" "<<y<<" "<<z;
if (x == -1 || y == -1 || z == -1) {
cout<<-1; return 0;
}
ll mn = d[z] - x + 1 - 3 * mx;// cout<<mn;
while (d[x] != -1) {
x = a[x];
if (d[x] == -1) break;
while (d[x] > y && d[y] != -1) {
y = a[y];
}
if(d[y] == -1) break;
while(d[y] > z && d[z] != -1) {
z = a[z];
}
if (d[z] == -1) break;
mn = min(mn , d[z] - x + 1 - 3 * mx);
}
cout<<mn;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2080 ms |
212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2080 ms |
212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2080 ms |
212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |