#include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = 20007;
int J[MAXN];
int O[MAXN];
int I[MAXN];
int main()
{
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<int> vJ;
vector<int> vO;
vector<int> vI;
for(int i = 0; i < n; ++i)
{
if(s[i] == 'J')
vJ.push_back(i);
if(s[i] == 'O')
vO.push_back(i);
if(s[i] == 'I')
vI.push_back(i);
}
for(int i = 0; i < (int)vJ.size() - k + 1; ++i)
J[vJ[i]] = vJ[i+k-1];
for(int i = 0; i < (int)vO.size() - k + 1; ++i)
O[vO[i]] = vO[i+k-1];
for(int i = 0; i < (int)vI.size() - k + 1; ++i)
I[vI[i]] = vI[i+k-1];
int naso[n];
int nasi[n];
int oo = n;
int ii = n;
for(int i = n-1; i >= 0; --i)
{
naso[i] = oo;
nasi[i] = ii;
if(s[i] == 'O')
oo = i;
if(s[i] == 'I')
ii = i;
}
int odp = MAXN;
for(int i = 0; i < n; ++i)
{
if(s[i] != 'J')
continue;
int a = J[i];
if(a == 0) continue;
int b = naso[a];
if(b == n) continue;
int c = O[b];
if(c == 0) continue;
int d = nasi[c];
if(d == n) continue;
int e = I[d];
if(e == 0) continue;
odp = min(odp, e-i+1);
}
if(odp == MAXN)
{
cout << "-1";
return 0;
}
cout << odp - k*3;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
448 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 |
336 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 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
448 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 |
336 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 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
448 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 |
336 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 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |