제출 #1119833

#제출 시각아이디문제언어결과실행 시간메모리
1119833staszic_ojuzJJOOII 2 (JOI20_ho_t2)C++17
100 / 100
8 ms3036 KiB
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int n, k;
string s;

vector<int> oblicz(char c)
{
	vector<int> poz(n + 2);

	int prawy = 0, licznik = 0;

	for (int lewy = 0; lewy < n; ++lewy)
	{
		if (lewy > 0 && s[lewy - 1] == c)
			licznik--;

		while (prawy < n && licznik < k)
		{
			if (s[prawy] == c)
				licznik++;
			prawy++;
		}

		poz[lewy] = (licznik == k ? prawy : n + 1);
	}

	poz[n] = poz[n + 1] = n + 1;
	return poz;
}

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

	cin >> n >> k >> s;

	vector<int> a = oblicz('J');
	vector<int> b = oblicz('O');
	vector<int> c = oblicz('I');

	int odp = n;
	for (int i = 0; i < n; ++i)
	{
		int x = c[b[a[i]]];
		if (x <= n)
			odp = min(odp, x - i - 3 * k);
	}

	cout << (odp == n ? -1 : odp) << '\n';
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...