Submission #423376

# Submission time Handle Problem Language Result Execution time Memory
423376 2021-06-11T04:20:02 Z milleniumEeee JJOOII 2 (JOI20_ho_t2) C++17
100 / 100
887 ms 2992 KB
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pii pair<int, int>
#define pb push_back
#define szof(s) (int)s.size()
#define all(s) s.begin(), s.end()
#define fastInp ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;

const int INF = 1e9;
const int MAXN = (int)2e5 + 5;

vector <char> vec = {'J', 'O', 'I', '#'};
int cnt[1000];
int pref[MAXN][3];
int n, k;
string s;

int f(char c) {
	if (c == 'J') {
		return 0;
	}
	if (c == 'O') {
		return 1;
	}
	if (c == 'I') {
		return 2;
	}
}

int get_cnt(int l, int r, int type) {
	if (l > r) {
		return 0;
	}
	if (l == 0) {
		return pref[r][type];
	} else {
		return pref[r][type] - pref[l - 1][type];
	}
}

int find_pos(int l, int r, char ch, int k) {
	if (get_cnt(l, r, f(ch)) >= k) {
		int bl = l - 1, br = r;
		while (br - bl > 1) {
			int bmid = (bl + br) >> 1;
			if (get_cnt(l, bmid, f(ch)) >= k) {
				br = bmid;
			} else {
				bl = bmid;
			}
		}
		return br;
	} else {
		return -1;
	}
}

bool can(int l, int r, int k) {
	int opt_j = find_pos(l, r, 'J', k);
	if (opt_j == -1) {
		return false;
	}
	int opt_o = find_pos(opt_j + 1, r, 'O', k);
	if (opt_o == -1) {
		return false;
	}
	int opt_i = find_pos(opt_o + 1, r, 'I', k);
	if (opt_i == -1) {
		return false;
	}
	return true;
}

signed main() {
	fastInp;
	cin >> n >> k;
	cin >> s;
	for (int i = 0; i < n; i++) {
		if (i > 0) {
			for (int j = 0; j < 3; j++) {
				pref[i][j] = pref[i - 1][j];
			}	
		}
		pref[i][f(s[i])]++;
	}
	int ans = INF;
	for (int l = 0; l < n; l++) {
		if (can(l, n - 1, k)) {
			int bl = l, br = n - 1;
			while (br - bl > 1) {
				int bmid = (bl + br) >> 1;
				if (can(l, bmid, k)) {
					br = bmid;
				} else {
					bl = bmid;
				}
			}
			ans = min(ans, (br - l + 1) - k * 3);
		}
	}
	if (ans == INF) {
		ans = -1;
	}
	cout << ans << endl;
}
/*
10 2
OJIJOIOIIJ
*/

Compilation message

ho_t2.cpp: In function 'int f(char)':
ho_t2.cpp:30:1: warning: control reaches end of non-void function [-Wreturn-type]
   30 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
15 Correct 4 ms 352 KB Output is correct
16 Correct 5 ms 332 KB Output is correct
17 Correct 5 ms 332 KB Output is correct
18 Correct 2 ms 332 KB Output is correct
19 Correct 5 ms 332 KB Output is correct
20 Correct 5 ms 460 KB Output is correct
21 Correct 3 ms 364 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 3 ms 332 KB Output is correct
34 Correct 2 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
15 Correct 4 ms 352 KB Output is correct
16 Correct 5 ms 332 KB Output is correct
17 Correct 5 ms 332 KB Output is correct
18 Correct 2 ms 332 KB Output is correct
19 Correct 5 ms 332 KB Output is correct
20 Correct 5 ms 460 KB Output is correct
21 Correct 3 ms 364 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 3 ms 332 KB Output is correct
34 Correct 2 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 586 ms 2792 KB Output is correct
37 Correct 647 ms 2976 KB Output is correct
38 Correct 644 ms 2972 KB Output is correct
39 Correct 887 ms 2952 KB Output is correct
40 Correct 316 ms 2904 KB Output is correct
41 Correct 762 ms 2904 KB Output is correct
42 Correct 875 ms 2984 KB Output is correct
43 Correct 17 ms 1996 KB Output is correct
44 Correct 21 ms 2412 KB Output is correct
45 Correct 34 ms 2904 KB Output is correct
46 Correct 28 ms 2992 KB Output is correct
47 Correct 28 ms 2976 KB Output is correct
48 Correct 27 ms 2980 KB Output is correct
49 Correct 19 ms 2144 KB Output is correct
50 Correct 28 ms 2904 KB Output is correct
51 Correct 29 ms 2904 KB Output is correct
52 Correct 8 ms 2776 KB Output is correct
53 Correct 11 ms 2900 KB Output is correct
54 Correct 446 ms 2976 KB Output is correct
55 Correct 159 ms 2972 KB Output is correct
56 Correct 5 ms 2904 KB Output is correct