#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//s.order_of_key(), *s.find_by_order()
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef string str;
typedef long long ll;
#define int ll
typedef double db;
typedef long double ld;
typedef pair<int,int> pi;
#define fi first
#define se second
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
#define pb push_back
#define eb emplace_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
const int MOD = 1e9+7; //998244353
const ll INF = 1e18;
const int nx[4]={0,0,1,-1}, ny[4]={1,-1,0,0};
int32_t main(){
boost;
int N,K; cin>>N>>K;
str s;
cin>>s;
map <pair<char,int>,int> mp;
int pref[3][N+1];
memset(pref,0,sizeof(pref));
for(int i=1; i<=N; i++){
pref[0][i]+=pref[0][i-1]; pref[1][i]+=pref[1][i-1]; pref[2][i]+=pref[2][i-1];
if(s[i-1]=='J') pref[0][i]++;
else if(s[i-1]=='O') pref[1][i]++;
else pref[2][i]++;
}
/*for(int i=1; i<=N; i++) cout << pref[1][i] << ' ';
cout << endl;*/
int cnt=0;
for(int i=0; i<N;i++) if(s[i]=='J') mp[{s[i],++cnt}]=i;
cnt=0; for(int i=0; i<N;i++) if(s[i]=='O') mp[{s[i],++cnt}]=i;
cnt=0; for(int i=0; i<N;i++) if(s[i]=='I') mp[{s[i],++cnt}]=i;
//cout << mp[{'O',2}] << endl;
int res=INF;
for(int i=0;i<N; i++) if(s[i]=='J'){
int ans=0;
int l=i,r;
if(mp.count({'J',pref[0][l+1]+K-1})){
r=mp[{'J',pref[0][l+1]+K-1}];
ans+=pref[1][r+1]-pref[1][l]+pref[2][r+1]-pref[2][l];
l=r;
} else continue;
//cout << ans << endl;
if(pref[1][N]-pref[1][l+1]==0) continue;
r=mp[{'O',pref[1][l+1]+1}];
ans+=pref[0][r+1]-pref[0][l+1]+pref[2][r+1]-pref[2][l+1];
l=r;
//cout << ans << endl;
//cout << endl;
if(mp.count({'O',pref[1][l+1]+K-1})){
r=mp[{'O',pref[1][l+1]+K-1}];
ans+=pref[0][r+1]-pref[0][l]+pref[2][r+1]-pref[2][l];
l=r;
} else continue;
//cout << ans << endl;
if(pref[2][N]-pref[2][l+1]==0) continue;
r=mp[{'I',pref[2][l+1]+1}];
ans+=pref[0][r+1]-pref[0][l+1]+pref[1][r+1]-pref[1][l+1];
l=r;
/*cout << ans << endl;
cout << endl;*/
if(mp.count({'I',pref[2][l+1]+K-1})){
r=mp[{'I',pref[2][l+1]+K-1}];
ans+=pref[0][r+1]-pref[0][l]+pref[1][r+1]-pref[1][l];
l=r;
} else continue;
//cout << ans << endl;
res=min(ans,res);
}
if(res==INF) cout << -1 << endl;
else cout << res << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
6 ms |
640 KB |
Output is correct |
17 |
Correct |
6 ms |
640 KB |
Output is correct |
18 |
Correct |
5 ms |
512 KB |
Output is correct |
19 |
Correct |
6 ms |
768 KB |
Output is correct |
20 |
Correct |
6 ms |
640 KB |
Output is correct |
21 |
Correct |
5 ms |
640 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
5 ms |
512 KB |
Output is correct |
24 |
Correct |
6 ms |
640 KB |
Output is correct |
25 |
Correct |
6 ms |
640 KB |
Output is correct |
26 |
Correct |
5 ms |
640 KB |
Output is correct |
27 |
Correct |
5 ms |
640 KB |
Output is correct |
28 |
Correct |
6 ms |
640 KB |
Output is correct |
29 |
Correct |
5 ms |
640 KB |
Output is correct |
30 |
Correct |
6 ms |
640 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
640 KB |
Output is correct |
33 |
Correct |
5 ms |
640 KB |
Output is correct |
34 |
Correct |
6 ms |
640 KB |
Output is correct |
35 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
6 ms |
640 KB |
Output is correct |
17 |
Correct |
6 ms |
640 KB |
Output is correct |
18 |
Correct |
5 ms |
512 KB |
Output is correct |
19 |
Correct |
6 ms |
768 KB |
Output is correct |
20 |
Correct |
6 ms |
640 KB |
Output is correct |
21 |
Correct |
5 ms |
640 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
5 ms |
512 KB |
Output is correct |
24 |
Correct |
6 ms |
640 KB |
Output is correct |
25 |
Correct |
6 ms |
640 KB |
Output is correct |
26 |
Correct |
5 ms |
640 KB |
Output is correct |
27 |
Correct |
5 ms |
640 KB |
Output is correct |
28 |
Correct |
6 ms |
640 KB |
Output is correct |
29 |
Correct |
5 ms |
640 KB |
Output is correct |
30 |
Correct |
6 ms |
640 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
640 KB |
Output is correct |
33 |
Correct |
5 ms |
640 KB |
Output is correct |
34 |
Correct |
6 ms |
640 KB |
Output is correct |
35 |
Correct |
5 ms |
640 KB |
Output is correct |
36 |
Correct |
122 ms |
16772 KB |
Output is correct |
37 |
Correct |
138 ms |
18004 KB |
Output is correct |
38 |
Correct |
140 ms |
18052 KB |
Output is correct |
39 |
Correct |
141 ms |
17796 KB |
Output is correct |
40 |
Correct |
117 ms |
17924 KB |
Output is correct |
41 |
Correct |
131 ms |
17924 KB |
Output is correct |
42 |
Correct |
140 ms |
18052 KB |
Output is correct |
43 |
Correct |
65 ms |
11256 KB |
Output is correct |
44 |
Correct |
84 ms |
13956 KB |
Output is correct |
45 |
Correct |
109 ms |
18052 KB |
Output is correct |
46 |
Correct |
110 ms |
17924 KB |
Output is correct |
47 |
Correct |
109 ms |
17924 KB |
Output is correct |
48 |
Correct |
110 ms |
17924 KB |
Output is correct |
49 |
Correct |
72 ms |
12032 KB |
Output is correct |
50 |
Correct |
108 ms |
17924 KB |
Output is correct |
51 |
Correct |
109 ms |
17924 KB |
Output is correct |
52 |
Correct |
77 ms |
17284 KB |
Output is correct |
53 |
Correct |
86 ms |
17944 KB |
Output is correct |
54 |
Correct |
109 ms |
17924 KB |
Output is correct |
55 |
Correct |
88 ms |
17924 KB |
Output is correct |
56 |
Correct |
72 ms |
17924 KB |
Output is correct |