제출 #276804

#제출 시각아이디문제언어결과실행 시간메모리
276804islingrJJOOII 2 (JOI20_ho_t2)C++17
100 / 100
7 ms768 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)
 
const int N = 1 << 18, inf = 1e9;
char s[N];
int ci[N], co[N], cj[N];
 
signed main() {
	cin.tie(nullptr)->sync_with_stdio(false);
 
	int n, k, res = inf; cin >> n >> k >> s;
	for (int l = 0, j = 0, o = 0, i = 0, cj = 0, co = 0, ci = 0; l < n; ++l) {
		while (j < n && cj < k) {
			if (s[j] == 'J') ++cj;
			if (s[j] == 'O') --co;
			++j;
		}
		while (o < n && co < k) {
			if (s[o] == 'O') ++co;
			if (s[o] == 'I') --ci;
			++o;
		}
		while (i < n && ci < k) if (s[i++] == 'I') ++ci;
		if (ci != k) break;
		res = min(res, i - l - 3 * k);
		if (s[l] == 'J') --cj;
	}
	cout << (res < inf ? res : -1);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...