This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 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 (x == -1 || d[x] == -1) break;
while (d[x] > y && y != -1 && d[y] != -1) {
y = a[y];
}
if(y == -1 || d[y] == -1) break;
while(d[y] > z && z != -1 && d[z] != -1) {
z = a[z];
}
if (z == -1 || d[z] == -1) break;
mn = min(mn , d[z] - x + 1 - 3 * mx);
}
cout<<mn;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |