This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <fstream>
#include <iterator>
#include <set>
#include <map>
#include <unordered_map>
#include <iomanip>
#include <cctype>
#include <string>
#include <cassert>
#include <set>
#include <bitset>
#include <unordered_set>
using ll = int64_t;
#define pb push_back
#define all(a) a.begin(),a.end()
#define ppi pair<pair<int,int>,int>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
//#define int int64_t
// xcode cant include bits/stdc++.h
using namespace std;
//ifstream fin ("diff.in");
//ofstream fout ("diff.out");
/* /\_/\
* (= ._.)
* / > \>
*/
// encouraging cat
const int INF = 1000000007;
const int mod = 1000000007;
int32_t main() {
int n,k;
cin >> n >> k;
string s;
cin >> s;
vector<int> costJ(n,INF);
vector<int> costI(n,INF);
int cost = 0;
int first = -1;
int cnt = 0;
for (int i = 0;i < n;i++)
{
if (first != -1)
cost++;
if (s[i] == 'J')
{
cnt++;
if (first == -1)
{
first = i;
cost++;
}
cost--;
}
if (cnt > k)
{
cnt--;
first++;
while (s[first] != 'J')
{
cost--;
first++;
}
}
if (i == 0)
{
if (cnt == n)
costJ[0] = cost;
}
else if (cnt == k)
{
costJ[i] = min(costJ[i - 1] + 1,cost);
}
else
{
costJ[i] = costJ[i - 1];
}
}
cost = 0;
first = -1;
cnt = 0;
for (int i = n - 1;i >= 0;i--)
{
if (first != -1)
cost++;
if (s[i] == 'I')
{
cnt++;
if (first == -1)
{
first = i;
cost++;
}
cost--;
}
if (cnt > k)
{
cnt--;
first--;
while (s[first] != 'I')
{
cost--;
first--;
}
}
if (i == n - 1)
{
if (cnt == n)
costI[0] = cost;
}
else if (cnt == k)
{
costI[i] = min(costI[i + 1] + 1,cost);
}
else
{
costI[i] = costI[i + 1];
}
cnt = cnt;
}
cost = 0;
first = -1;
cnt = 0;
vector<pair<int,int>> costO(n,{INF,INF});
for (int i = 0;i < n;i++)
{
if (first != -1)
cost++;
if (s[i] == 'O')
{
cnt++;
if (first == -1)
{
first = i;
cost++;
}
cost--;
}
if (cnt > k)
{
cnt--;
first++;
while (s[first] != 'O')
{
cost--;
first++;
}
}
if (i == 0)
{
if (cnt == n)
costO[0] = {cost,0};
}
else if (cnt == k)
{
costO[i] = {min(costO[i - 1].first + 1,cost),first};
}
else
{
costO[i] = costO[i - 1];
}
}
int res = INF;
for (int i = 1;i < n - 1;i++)
{
if (costO[i].first != INF && costO[i].second - 1 >= 0 && costJ[costO[i].second - 1] != INF && costI[i + 1] != INF)
res = min(res,costO[i].first + costJ[costO[i].second - 1] + costI[i + 1]);
}
if (res == INF)
cout << -1 << '\n';
else
cout << res << '\n';
return 0;
}
Compilation message (stderr)
ho_t2.cpp:32:9: warning: "/*" within comment [-Wcomment]
32 | /* /\_/\
|
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |