This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize("-Ofast")
#include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <vector>
#include <limits>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <math.h>
using namespace std;
using ll = long long;
void solve(){
ll n, k;
string s;
cin >> n >> k;
cin >> s;
vector<ll> Os(n + 1, 0);
vector<ll> Js(n, 1E9);
vector<ll> Is(n, 1E9);
// Filling Js and Os
ll curr = 0;
ll picked_up = 0;
vector<ll> temp_list;
ll req_o = (k + 1)/2;
ll taken_o = 0;
for(ll i = 0; i < n; i++){
Os[i + 1] = Os[i];
if(s[i] == 'O'){
Os[i + 1]++;
taken_o++;
}
if(s[i] == 'J'){
temp_list.push_back(i);
picked_up++;
if(picked_up > k){
curr++;
}
taken_o = 0;
}
if(taken_o >= req_o && picked_up >= k){
Js[i] = i + 1 - k - req_o - temp_list[curr];
}
}
curr = 0;
picked_up = 0;
temp_list.clear();
req_o = k/2;
taken_o = 0;
ll curr_min = 1E9;
for(ll i = n - 1; i > 0; i--){
if(s[i] == 'O'){
taken_o++;
}
if(s[i] == 'I'){
temp_list.push_back(n - i - 1);
picked_up++;
if(picked_up > k){
curr++;
}
taken_o = 0;
}
if(taken_o >= req_o && picked_up >= k){
Is[i] = n - i - k - temp_list[curr] - req_o;
curr_min = min(Js[i - 1] + Is[i], curr_min);
}
}
if(curr_min >= 1E8){
cout << - 1;
}else{
cout << curr_min;
}
//cout << "lol" << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
ll t = 1;
while(t--){
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |