Submission #387944

# Submission time Handle Problem Language Result Execution time Memory
387944 2021-04-09T14:45:07 Z nikatamliani JJOOII 2 (JOI20_ho_t2) C++14
100 / 100
8 ms 3292 KB
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll N = 5e5+5, oo = 1e18+10;
ll n, k, cnt[N];
string s;
ll sum(ll l, ll r) {
	return cnt[r] - cnt[l - 1];
}
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n >> k >> s;
	vector<int> I, J;
	for(int i = 0; i < n; ++i) {
		if(s[i] == 'J') {
			J.push_back(i);
		}
		if(s[i] == 'I') {
			I.push_back(i);
		}
		cnt[i] = cnt[i - 1] + (s[i] == 'O');
	}
	ll ans = oo;
	for(int i = k - 1, j = 0; i < (int)J.size(); ++i) {
		while(j+k <= (int)I.size() && (I[j] < J[i] || sum(J[i], I[j]) < k)) ++j;
		if(j+k <= (int)I.size() && sum(J[i], I[j]) >= k) {
			ll lft = J[i] - J[i - k + 1] + 1 - k;
			ll rgh = I[j+k-1] - I[j] + 1 - k;
			ll mid = I[j] - J[i] - 1 - k;
			ans = min(ans, lft + mid + rgh);
		} 
	}
	if(ans == oo) {
		ans = -1;
	}
	cout << ans << endl;
}
# 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 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 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 208 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 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 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 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 208 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 336 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 1 ms 340 KB Output is correct
34 Correct 1 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 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 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 208 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 336 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 1 ms 340 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 6 ms 2680 KB Output is correct
37 Correct 8 ms 3168 KB Output is correct
38 Correct 6 ms 3280 KB Output is correct
39 Correct 6 ms 3172 KB Output is correct
40 Correct 7 ms 3076 KB Output is correct
41 Correct 8 ms 3164 KB Output is correct
42 Correct 6 ms 3124 KB Output is correct
43 Correct 3 ms 2128 KB Output is correct
44 Correct 5 ms 2388 KB Output is correct
45 Correct 5 ms 3116 KB Output is correct
46 Correct 5 ms 3148 KB Output is correct
47 Correct 5 ms 3108 KB Output is correct
48 Correct 5 ms 3172 KB Output is correct
49 Correct 4 ms 2116 KB Output is correct
50 Correct 5 ms 3180 KB Output is correct
51 Correct 5 ms 3084 KB Output is correct
52 Correct 6 ms 2868 KB Output is correct
53 Correct 5 ms 3172 KB Output is correct
54 Correct 6 ms 3280 KB Output is correct
55 Correct 4 ms 3292 KB Output is correct
56 Correct 5 ms 3292 KB Output is correct