Submission #312574

#TimeUsernameProblemLanguageResultExecution timeMemory
312574TemmieJJOOII 2 (JOI20_ho_t2)C++17
100 / 100
11 ms1664 KiB
#include <bits/stdc++.h>

int main() {
	std::ios::sync_with_stdio(0); std::cin.tie(0);
	
	int n, k; std::cin >> n >> k;
	std::deque <int> J, O, I, a;
	for (int i = 0; i < n; i++) {
		char p; std::cin >> p;
		if (p == 'J') J.push_back(i);
		if (p == 'O') O.push_back(i);
		if (p == 'I') I.push_back(i);
	}
	int ans = 1 << 30;
	for (int i = 0, j = 0; j + k <= (int)O.size(); j++) {
		int l = O[j], r = O[j + k - 1];
		while (i < (int)J.size() && J[i] < l) a.push_back(J[i++]);
		while ((int)a.size() > k) a.pop_front();
		while (I.size() && I.front() < r) I.pop_front();
		if ((int)a.size() == k && (int)I.size() >= k) ans = std::min(ans, I[k - 1] - a[0] + 1);
	}
	std::cout << (ans == (1 << 30) ? -1 : ans - k * 3) << "\n";
	
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...