#include<bits/stdc++.h>
//#define int long long
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define fi first
#define se second
#define ist insert
using namespace std;
int min(int a,int b){return a<b?a:b;}
int max(int a,int b){return a>b?a:b;}
typedef long long ll;
typedef pair<ll,ll> pii;
const int N=200005;
const int MOD=1000000007;//998244353
const int INF=2147483647;//1700000000000000000
int n,k;
string s;
vector<int> site[3];
int a[N];
vector<pii> dp[3];
bool vaild(){
int cnt=0,typ=0;
for (int i=0;i<n;i++){
if (typ==0&&s[i]=='J') cnt++;
if (typ==1&&s[i]=='O') cnt++;
if (typ==2&&s[i]=='I') cnt++;
if (cnt==k) cnt=0,typ++;
}
return typ==3;
}
int srch1(int x){
int l=0,r=dp[0].size(),res=-1;
while (r>=l){
int mid=(l+r)/2;
if (dp[0][mid].fi<x){
res=mid;
l=mid+1;
}
else r=mid-1;
}
return res;
}
int srch2(int x){
int l=0,r=dp[2].size(),res=-1;
while (r>=l){
int mid=(l+r)/2;
if (dp[2][mid].fi>x){
res=mid;
r=mid-1;
}
else l=mid+1;
}
return res;
}
inline void sol(){
cin >>n>>k>>s;
if (!vaild()){
cout <<"-1\n";
return;
}
for (int i=0;i<n;i++){
if (s[i]=='J') a[i]=0;
if (s[i]=='O') a[i]=1;
if (s[i]=='I') a[i]=2;
site[a[i]].pb(i);
}
int ans=INF,j=k-1;
for (int t=k-1;t<site[0].size();t++){
int i=t-k+1;
while (j<site[2].size()&&(lower_bound(ALL(site[1]),site[2][j-k+1])-lower_bound(ALL(site[1]),site[0][t]))<k) j++;
if (j==site[2].size()) break;
ans=min(ans,site[2][j]-site[0][i]+1-(k*3));
/*for (int j=k-1;j<site[2].size();j++){
if ((lower_bound(ALL(site[1]),site[2][j-k+1])-lower_bound(ALL(site[1]),site[0][t]))<k||site[0][i]>site[2][j]) continue;
//cout <<site[2][j]<<' '<<site[0][i]<<'\n';
ans=min(ans,site[2][j]-site[0][i]+1-(k*3));
}*/
}
cout <<ans<<'\n';
}
signed main(){
int _=1;
//cin >>_;
while (_--) sol();
return 0;
}
Compilation message
ho_t2.cpp: In function 'void sol()':
ho_t2.cpp:70:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (int t=k-1;t<site[0].size();t++){
| ~^~~~~~~~~~~~~~~
ho_t2.cpp:72:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | while (j<site[2].size()&&(lower_bound(ALL(site[1]),site[2][j-k+1])-lower_bound(ALL(site[1]),site[0][t]))<k) j++;
| ~^~~~~~~~~~~~~~~
ho_t2.cpp:73:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | if (j==site[2].size()) break;
| ~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
304 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
304 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
17 ms |
2108 KB |
Output is correct |
37 |
Correct |
17 ms |
2456 KB |
Output is correct |
38 |
Correct |
17 ms |
2476 KB |
Output is correct |
39 |
Correct |
18 ms |
2500 KB |
Output is correct |
40 |
Correct |
15 ms |
2528 KB |
Output is correct |
41 |
Correct |
17 ms |
2468 KB |
Output is correct |
42 |
Correct |
20 ms |
2432 KB |
Output is correct |
43 |
Correct |
7 ms |
1624 KB |
Output is correct |
44 |
Correct |
9 ms |
1876 KB |
Output is correct |
45 |
Correct |
12 ms |
2484 KB |
Output is correct |
46 |
Correct |
11 ms |
2572 KB |
Output is correct |
47 |
Correct |
11 ms |
2508 KB |
Output is correct |
48 |
Correct |
11 ms |
2520 KB |
Output is correct |
49 |
Correct |
3 ms |
596 KB |
Output is correct |
50 |
Correct |
5 ms |
612 KB |
Output is correct |
51 |
Correct |
5 ms |
612 KB |
Output is correct |
52 |
Correct |
5 ms |
612 KB |
Output is correct |
53 |
Correct |
5 ms |
596 KB |
Output is correct |
54 |
Correct |
10 ms |
2528 KB |
Output is correct |
55 |
Correct |
9 ms |
2484 KB |
Output is correct |
56 |
Correct |
8 ms |
2504 KB |
Output is correct |