#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define FOR(i, l, r, d) for(int i=(l); i<=(r); i+=(d))
#define szof(x) ((int)(x).size())
#define vi vector<int>
#define pii pair<int, int>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mkp make_pair
const int INF = INT_MAX;
const int LNF = INF*INF;
const int MOD = 1000000007;
const double eps = 1e-12;
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
const int MAX = 200010;
int n, k;
int pref[4][MAX]; // J = 1, O = 2, I = 3
int jl, jr, il, ir;
int res = INF;
inline int query(int ql, int qr, char qc){
if(qc == 'J') return pref[1][qr] - pref[1][ql-1];
if(qc == 'O') return pref[2][qr] - pref[2][ql-1];
return pref[3][qr] - pref[3][ql-1];
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
FOR(i, 1, n, 1){
char owo;
cin>>owo;
FOR(j, 1, 3, 1) pref[j][i] = pref[j][i-1];
if(owo == 'J') pref[1][i]++;
if(owo == 'O') pref[2][i]++;
if(owo == 'I') pref[3][i]++;
}
FOR(j, 1, 3, 1) pref[j][n+1] = INF;
//cerr<<"ok"<<endl;
jl = 1, jr = 0, il = 0, ir = 0;
while(jl <= n){
// move [jl, jr]
while(jr <= n and query(jl, jr, 'J') < k) jr++;
il = max(il, jr + 1);
while(ir <= n and query(il, ir, 'I') < k) ir++;
if(ir > n) break;
//cerr<<"? "<<'['<<jl<<", "<<jr<<"] ["<<il<<", "<<ir<<"] "<<endl;
// move [il, ir] until [jr+1, ir-1] is ok
while(query(jr+1, il-1, 'O') < k){
il++;
while(ir <= n and query(il, ir, 'I') < k) ir++;
if(ir > n) break;
//cerr<<". "<<'['<<jl<<", "<<jr<<"] ["<<il<<", "<<ir<<"] "<<endl;
}
if(ir > n) break;
// update res
res = min(res, ir - jl + 1 - 3*k);
//cerr<<"! "<<'['<<jl<<", "<<jr<<"] ["<<il<<", "<<ir<<"] "<<endl;
jl++;
}
if(res < INF) cout<<res<<'\n';
else cout<<-1<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
0 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
0 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
300 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
324 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
324 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
324 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
324 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
320 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
328 KB |
Output is correct |
35 |
Correct |
0 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
0 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
300 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
324 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
324 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
324 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
324 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
320 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
328 KB |
Output is correct |
35 |
Correct |
0 ms |
328 KB |
Output is correct |
36 |
Correct |
10 ms |
4796 KB |
Output is correct |
37 |
Correct |
10 ms |
5068 KB |
Output is correct |
38 |
Correct |
14 ms |
5072 KB |
Output is correct |
39 |
Correct |
14 ms |
5068 KB |
Output is correct |
40 |
Correct |
10 ms |
5068 KB |
Output is correct |
41 |
Correct |
10 ms |
5068 KB |
Output is correct |
42 |
Correct |
11 ms |
5196 KB |
Output is correct |
43 |
Correct |
8 ms |
3320 KB |
Output is correct |
44 |
Correct |
6 ms |
4044 KB |
Output is correct |
45 |
Correct |
8 ms |
5196 KB |
Output is correct |
46 |
Correct |
8 ms |
5108 KB |
Output is correct |
47 |
Correct |
9 ms |
5188 KB |
Output is correct |
48 |
Correct |
8 ms |
5144 KB |
Output is correct |
49 |
Correct |
5 ms |
3460 KB |
Output is correct |
50 |
Correct |
8 ms |
5200 KB |
Output is correct |
51 |
Correct |
8 ms |
5192 KB |
Output is correct |
52 |
Correct |
7 ms |
4940 KB |
Output is correct |
53 |
Correct |
8 ms |
5116 KB |
Output is correct |
54 |
Correct |
9 ms |
5104 KB |
Output is correct |
55 |
Correct |
8 ms |
5100 KB |
Output is correct |
56 |
Correct |
7 ms |
5188 KB |
Output is correct |