답안 #828132

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
828132 2023-08-17T05:36:52 Z rahulverma JJOOII 2 (JOI20_ho_t2) Java 11
100 / 100
252 ms 18440 KB
import java.io.*;
import java.util.*;

public class ho_t2 {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int n = Integer.parseInt(st.nextToken());
		int k = Integer.parseInt(st.nextToken());
		PrintWriter pw = new PrintWriter(System.out);
		String s = br.readLine();
		
		ArrayList<Integer> J = new ArrayList<Integer>();
		ArrayList<Integer> O = new ArrayList<Integer>();
		ArrayList<Integer> I = new ArrayList<Integer>();
		
		for(int i = 0; i < n; i++) {
			if(s.charAt(i) == 'J') J.add(i);
			else if(s.charAt(i) == 'O') O.add(i);
			else I.add(i);
		}
		if(J.size() < k || O.size() < k || I.size() < k) {
			pw.println(-1);
			pw.close();
			return;
		}
		
		int[] jPrefix = new int[J.size()];
		int[] oPrefix = new int[O.size()];
		int[] iPrefix = new int[I.size()];
		for(int i = 1; i < J.size(); i++) jPrefix[i] = jPrefix[i - 1] + J.get(i) - J.get(i - 1) - 1;
		for(int i = 1; i < O.size(); i++) oPrefix[i] = oPrefix[i - 1] + O.get(i) - O.get(i - 1) - 1;
		for(int i = 1; i < I.size(); i++) iPrefix[i] = iPrefix[i - 1] + I.get(i) - I.get(i - 1) - 1;

		
		int ans = n + 1;
		for(int i = 0; i < J.size() - k + 1; i++) {
			int cur = 0;
			int add = k - 1;
			int max = J.get(i + add);
			cur += jPrefix[i + add] - jPrefix[i];
			int left = 0;
			int right = O.size();
			while(left < right) {
				int mid = left + (right - left) / 2;
				if(O.get(mid) < max) {
					left = mid + 1;
				}
				else {
					right = mid;
				}
			}
			if(O.size() - left < k) {
				continue;
			}
			cur += O.get(left) - J.get(i + add) - 1;
			cur += oPrefix[left + add] - oPrefix[left];
			int val = O.get(left + add);
			int og = left;
			left = 0;
			right = I.size();
			while(left < right) {
				int mid = left + (right - left) / 2;
				if(I.get(mid) < val) {
					left = mid + 1;
				}
				else {
					right = mid;
				}
			}
			if(I.size() - left < k) {
				continue;
			}
			cur += I.get(left) - O.get(og + add) - 1;
			cur += iPrefix[left + add] - iPrefix[left];
			ans = Math.min(ans, cur);
		}
		if(ans == n + 1) {
			pw.println(-1);
		}
		else {
			pw.println(ans);
		}
		pw.close();
	}

}
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 8556 KB Output is correct
2 Correct 52 ms 8356 KB Output is correct
3 Correct 49 ms 8288 KB Output is correct
4 Correct 51 ms 8276 KB Output is correct
5 Correct 49 ms 8360 KB Output is correct
6 Correct 49 ms 8380 KB Output is correct
7 Correct 49 ms 8564 KB Output is correct
8 Correct 48 ms 8504 KB Output is correct
9 Correct 48 ms 8296 KB Output is correct
10 Correct 56 ms 8256 KB Output is correct
11 Correct 49 ms 8176 KB Output is correct
12 Correct 49 ms 8136 KB Output is correct
13 Correct 48 ms 8484 KB Output is correct
14 Correct 50 ms 8268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 8556 KB Output is correct
2 Correct 52 ms 8356 KB Output is correct
3 Correct 49 ms 8288 KB Output is correct
4 Correct 51 ms 8276 KB Output is correct
5 Correct 49 ms 8360 KB Output is correct
6 Correct 49 ms 8380 KB Output is correct
7 Correct 49 ms 8564 KB Output is correct
8 Correct 48 ms 8504 KB Output is correct
9 Correct 48 ms 8296 KB Output is correct
10 Correct 56 ms 8256 KB Output is correct
11 Correct 49 ms 8176 KB Output is correct
12 Correct 49 ms 8136 KB Output is correct
13 Correct 48 ms 8484 KB Output is correct
14 Correct 50 ms 8268 KB Output is correct
15 Correct 60 ms 8440 KB Output is correct
16 Correct 71 ms 8648 KB Output is correct
17 Correct 60 ms 8360 KB Output is correct
18 Correct 56 ms 8748 KB Output is correct
19 Correct 71 ms 8664 KB Output is correct
20 Correct 59 ms 8552 KB Output is correct
21 Correct 59 ms 8692 KB Output is correct
22 Correct 58 ms 8568 KB Output is correct
23 Correct 59 ms 8444 KB Output is correct
24 Correct 58 ms 8664 KB Output is correct
25 Correct 59 ms 8548 KB Output is correct
26 Correct 60 ms 8412 KB Output is correct
27 Correct 62 ms 8560 KB Output is correct
28 Correct 58 ms 8704 KB Output is correct
29 Correct 67 ms 8348 KB Output is correct
30 Correct 61 ms 8668 KB Output is correct
31 Correct 52 ms 8332 KB Output is correct
32 Correct 59 ms 8396 KB Output is correct
33 Correct 74 ms 8712 KB Output is correct
34 Correct 61 ms 8496 KB Output is correct
35 Correct 57 ms 8336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 8556 KB Output is correct
2 Correct 52 ms 8356 KB Output is correct
3 Correct 49 ms 8288 KB Output is correct
4 Correct 51 ms 8276 KB Output is correct
5 Correct 49 ms 8360 KB Output is correct
6 Correct 49 ms 8380 KB Output is correct
7 Correct 49 ms 8564 KB Output is correct
8 Correct 48 ms 8504 KB Output is correct
9 Correct 48 ms 8296 KB Output is correct
10 Correct 56 ms 8256 KB Output is correct
11 Correct 49 ms 8176 KB Output is correct
12 Correct 49 ms 8136 KB Output is correct
13 Correct 48 ms 8484 KB Output is correct
14 Correct 50 ms 8268 KB Output is correct
15 Correct 60 ms 8440 KB Output is correct
16 Correct 71 ms 8648 KB Output is correct
17 Correct 60 ms 8360 KB Output is correct
18 Correct 56 ms 8748 KB Output is correct
19 Correct 71 ms 8664 KB Output is correct
20 Correct 59 ms 8552 KB Output is correct
21 Correct 59 ms 8692 KB Output is correct
22 Correct 58 ms 8568 KB Output is correct
23 Correct 59 ms 8444 KB Output is correct
24 Correct 58 ms 8664 KB Output is correct
25 Correct 59 ms 8548 KB Output is correct
26 Correct 60 ms 8412 KB Output is correct
27 Correct 62 ms 8560 KB Output is correct
28 Correct 58 ms 8704 KB Output is correct
29 Correct 67 ms 8348 KB Output is correct
30 Correct 61 ms 8668 KB Output is correct
31 Correct 52 ms 8332 KB Output is correct
32 Correct 59 ms 8396 KB Output is correct
33 Correct 74 ms 8712 KB Output is correct
34 Correct 61 ms 8496 KB Output is correct
35 Correct 57 ms 8336 KB Output is correct
36 Correct 185 ms 18156 KB Output is correct
37 Correct 197 ms 18256 KB Output is correct
38 Correct 181 ms 18052 KB Output is correct
39 Correct 222 ms 18188 KB Output is correct
40 Correct 208 ms 18272 KB Output is correct
41 Correct 211 ms 18420 KB Output is correct
42 Correct 241 ms 18348 KB Output is correct
43 Correct 199 ms 16776 KB Output is correct
44 Correct 218 ms 18432 KB Output is correct
45 Correct 200 ms 18388 KB Output is correct
46 Correct 246 ms 18392 KB Output is correct
47 Correct 231 ms 18412 KB Output is correct
48 Correct 252 ms 18440 KB Output is correct
49 Correct 178 ms 16148 KB Output is correct
50 Correct 203 ms 18004 KB Output is correct
51 Correct 194 ms 17960 KB Output is correct
52 Correct 180 ms 17688 KB Output is correct
53 Correct 172 ms 17784 KB Output is correct
54 Correct 185 ms 18180 KB Output is correct
55 Correct 178 ms 17980 KB Output is correct
56 Correct 182 ms 17700 KB Output is correct