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 <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
const int MAXN = 200013;
const int INF = 1000000007;
int N, K;
int arr[MAXN];
vi pos[3];
int ans = INF;
int32_t main()
{
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> K;
string temps; cin >> temps;
FOR(i, 0, N)
{
if (temps[i] == 'J') arr[i] = 0;
if (temps[i] == 'O') arr[i] = 1;
if (temps[i] == 'I') arr[i] = 2;
}
//when is it ok to start the I?
//when is it ok to start the O?
//when is it ok to start the J?
FOR(i, 0, N)
{
pos[arr[i]].PB(i);
}
int it1 = 0, it2 = 0;
for (int i = 0; i <= SZ(pos[0]) - K; i++)
{
while(it1 < SZ(pos[1]) && pos[1][it1] < pos[0][i + K - 1]) it1++;
if (it1 + K - 1 >= SZ(pos[1])) break;
while(it2 < SZ(pos[2]) && pos[2][it2] < pos[1][it1 + K - 1]) it2++;
if (it2 + K - 1 >= SZ(pos[2])) break;
int lo = pos[0][i], hi = pos[2][it2 + K - 1];
ckmin(ans, hi - lo + 1 - 3 * K);
}
if (ans >= INF)
{
cout << "-1\n";
return 0;
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |