Submission #244874

#TimeUsernameProblemLanguageResultExecution timeMemory
244874maximath_1JJOOII 2 (JOI20_ho_t2)C++11
0 / 100
5 ms384 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int cnt[3][200005], last[3][200005], n, k;
string s;

void proc(int id){
	int lf = 0;
	for(int rg = 1; rg <= n; rg ++){
		while(cnt[id][rg] - cnt[id][lf] >= k) lf ++;
		last[id][rg - 1] = lf - 1;
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	cin >> n >> k;
	cin >> s;
	for(int i = 0; i < n; i ++){
		if(s[i] == 'J') cnt[0][i + 1]++;
		if(s[i] == 'O') cnt[1][i + 1]++;
		if(s[i] == 'I') cnt[2][i + 1]++;
	}

	for(int i = 1; i <= n; i ++) for(int j = 0; j < 3; j ++)
		cnt[j][i] += cnt[j][i - 1];

	for(int i = 0; i < 3; i ++) proc(i);

	int ans = 2000000000;

	for(int i = 0; i < n; i ++){
		int nw = last[2][i];
		nw --;
		if(nw >= 0) nw = last[1][nw];
		else nw = -1000000;
		nw --;
		if(nw >= 0) nw = last[0][nw];
		else nw = -1000000;

		if(nw != -1000000)
			ans = min(ans, i - nw + 1 - 3 * k);
	}

	if(ans == 2000000000) ans = -1;
	printf("%d\n", ans);
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...