Skip to content

Commit b12948b

Browse files
committed
[BOJ]IF문 좀 대신 써줘/실버3/1시간
1 parent c037050 commit b12948b

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//BOJ19637 IF문 좀 대신 써줘, 실버3
2+
//hashmap or class 만들어서 ArrayList에 넣는 자료구조를 사용하면 되지 않을까라는 생각이
3+
//제일 처음 들었음 - 이렇게 하니 for문을 2중으로 써서 그런지 시간초과뜸
4+
//이분 탐색으로 시도
5+
//문제를 이분 탐색과 연결해서 생각하는 힘이 아직 부족하다.
6+
import java.io.*;
7+
import java.util.*;
8+
9+
public class BOJ19637 {
10+
static int N, M;
11+
static String[] title;
12+
static int[] power;
13+
public static void main(String[] args) throws IOException {
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
16+
StringTokenizer st = new StringTokenizer(br.readLine());
17+
18+
N = Integer.parseInt(st.nextToken());
19+
M = Integer.parseInt(st.nextToken());
20+
title = new String[N];
21+
power = new int[N];
22+
23+
for(int i = 0; i < N; i++) {
24+
st = new StringTokenizer(br.readLine());
25+
title[i] = st.nextToken();
26+
power[i] = Integer.parseInt(st.nextToken());
27+
}
28+
29+
//이게 실패 코드 - 일반적인 for문으로 탐색하는 방법인데 시간초과 뜸
30+
// for(int i = 0; i < M; i++) {
31+
// int num = Integer.parseInt(br.readLine());
32+
// for(int j = 0; j < N; j++) {
33+
// if(num <= power[j]) {
34+
//// System.out.println(title[j] + " " + num + " " + power[j]);
35+
// bw.write(title[j]+"\n");
36+
// break;
37+
// }
38+
// }
39+
// }
40+
41+
//이분 탐색 코드
42+
for(int i = 0; i < M; i++) {
43+
int num = Integer.parseInt(br.readLine());
44+
45+
int start = 0;
46+
int end = N-1;
47+
48+
while(start <= end) {
49+
int mid = (start+end) / 2;
50+
if(power[mid] < num) {
51+
start = mid + 1;
52+
}else {
53+
end = mid - 1;
54+
}
55+
}
56+
bw.write(title[start]+"\n");
57+
}
58+
59+
bw.flush();
60+
bw.close();
61+
br.close();
62+
}
63+
}

0 commit comments

Comments
 (0)