#include<bits/stdc++.h>
#define fori(i,a,b) for(int i=a;i<=b;i++)
#define int long long
#define ii pair<int,int>
#define fi first
#define se second
#define pb push_back
using namespace std;
const int mod=1e9+7;
const int M=2e5+10;
const int N=1e3+10;
int n,k;
string s;
int sum[4][M];
char ch[4]={'J','O','I'};
bool check(int l,int r)
{
fori(i,0,2) if(sum[i][r]-sum[i][l-1]<k) return false;
return true;
}
int id(char ch2)
{
fori(i,0,2) if(ch2==ch[i]) return i;
}
vector<int> v[4];
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define task "1"
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n >> k;
cin >> s;
s=' '+s;
fori(i,1,n) v[id(s[i])].pb(i);
fori(i,0,n+1)
{
sum[0][i]=-1e9;
sum[2][i]=1e9;
}
int kq=1e9;
for(int i=k-1;i<=v[0].size()-1;i++) sum[0][v[0][i]]=v[0][i-k+1];
fori(i,1,n) sum[0][i]=max(sum[0][i-1],sum[0][i]);
for(int i=v[2].size()-k;i>=0;i--) sum[2][v[2][i]]=v[2][i+k-1];
for(int i=n;i!=0;i--) sum[2][i]=min(sum[2][i],sum[2][i+1]);
if(v[1].size())
for(int i=k-1;i<=v[1].size()-1;i++)
{
kq=min(kq,sum[2][v[1][i]]-sum[0][v[1][i-k+1]]+1);
//cout << sum[2][v[1][i]] << " " << sum[0][v[1][i-k+1]] << "\n";
}
cout << (kq>n ? -1 :kq-3*k);
}
Compilation message
ho_t2.cpp: In function 'int32_t main()':
ho_t2.cpp:47:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int i=k-1;i<=v[0].size()-1;i++) sum[0][v[0][i]]=v[0][i-k+1];
| ~^~~~~~~~~~~~~~~
ho_t2.cpp:52:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i=k-1;i<=v[1].size()-1;i++)
| ~^~~~~~~~~~~~~~~
ho_t2.cpp: In function 'long long int id(char)':
ho_t2.cpp:24:1: warning: control reaches end of non-void function [-Wreturn-type]
24 | }
| ^
ho_t2.cpp: In function 'int32_t main()':
ho_t2.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:35:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
464 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
2396 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
464 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
2396 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
6 ms |
5672 KB |
Output is correct |
37 |
Correct |
6 ms |
5688 KB |
Output is correct |
38 |
Correct |
6 ms |
5488 KB |
Output is correct |
39 |
Correct |
6 ms |
6536 KB |
Output is correct |
40 |
Correct |
6 ms |
5608 KB |
Output is correct |
41 |
Correct |
6 ms |
6928 KB |
Output is correct |
42 |
Correct |
7 ms |
6552 KB |
Output is correct |
43 |
Correct |
4 ms |
4892 KB |
Output is correct |
44 |
Correct |
5 ms |
5704 KB |
Output is correct |
45 |
Correct |
6 ms |
6928 KB |
Output is correct |
46 |
Correct |
6 ms |
6928 KB |
Output is correct |
47 |
Correct |
6 ms |
6808 KB |
Output is correct |
48 |
Correct |
6 ms |
6888 KB |
Output is correct |
49 |
Correct |
4 ms |
5208 KB |
Output is correct |
50 |
Correct |
6 ms |
6928 KB |
Output is correct |
51 |
Correct |
6 ms |
6932 KB |
Output is correct |
52 |
Correct |
5 ms |
6692 KB |
Output is correct |
53 |
Correct |
6 ms |
6840 KB |
Output is correct |
54 |
Correct |
5 ms |
5596 KB |
Output is correct |
55 |
Correct |
6 ms |
5668 KB |
Output is correct |
56 |
Correct |
5 ms |
5532 KB |
Output is correct |