#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,k;
string napis;
cin >> n >> k >> napis;
queue<int>indexJ,indexI,indexO;
vector<int>J1,J2,I1,I2,O1,O2;
for(int i=0;i<n;++i){
if(napis[i]=='O')
indexO.push(i);
if(napis[i]=='J')
indexJ.push(i);
if(napis[i]=='I')
indexI.push(i);
if(indexJ.size()==k){
J1.push_back(indexJ.front());
J2.push_back(i);
indexJ.pop();
}
if(indexO.size()==k){
O1.push_back(indexO.front());
O2.push_back(i);
indexO.pop();
}
if(indexI.size()==k){
I1.push_back(indexI.front());
I2.push_back(i);
indexI.pop();
}
}
int wynik=2000007;
vector<int>::iterator it1,it2;
for(int i=0;i<J1.size();++i){
it1=lower_bound(O1.begin(),O1.end(),J2[i]);
if(it1!=O1.end()){
it2=lower_bound(I1.begin(),I1.end(),O2[it1-O1.begin()]);
//cout << O2[it1-O1.begin()];
if(it2!=I1.end()){
int s1=J2[i]-J1[i]+1-k;
int s2=O2[it1-O1.begin()]-O1[it1-O1.begin()]+1-k;
int s3=I2[it2-I1.begin()]-I1[it2-I1.begin()]+1-k;
//cout << s1 << ' ' << ' ' << s2 << ' '<< s3 << '\n';
wynik=min(s1+s2+s3,wynik);
}
}
}
if(wynik!=2000007)
cout << wynik;
else
cout << -1;
return 0;
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:19:25: warning: comparison of integer expressions of different signedness: 'std::queue<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
19 | if(indexJ.size()==k){
| ~~~~~~~~~~~~~^~~
ho_t2.cpp:24:25: warning: comparison of integer expressions of different signedness: 'std::queue<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
24 | if(indexO.size()==k){
| ~~~~~~~~~~~~~^~~
ho_t2.cpp:29:25: warning: comparison of integer expressions of different signedness: 'std::queue<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
29 | if(indexI.size()==k){
| ~~~~~~~~~~~~~^~~
ho_t2.cpp:37:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int i=0;i<J1.size();++i){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |