반응형
문제
https://www.acmicpc.net/problem/19637
JAVA코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
private static int N, M;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
StringBuilder sb = new StringBuilder();
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
List<Info> infoList = new ArrayList<>();
for (int idx = 0; idx < N; idx++) {
st = new StringTokenizer(br.readLine());
infoList.add(new Info(st.nextToken(), Integer.parseInt(st.nextToken())));
}
int val = 0;
int left, mid, right;
for (int idx = 0; idx < M; idx++) {
val = Integer.parseInt(br.readLine());
left = 0;
right = N - 1;
mid = right;
while (right - left > 1) {
mid = (right + left) / 2;
if (infoList.get(mid).getVal() >= val)
right = mid;
else
left = mid;
}
if (infoList.get(left).getVal() >= val)
sb.append(infoList.get(left).getStr() + "\n");
else
sb.append(infoList.get(right).getStr() + "\n");
}
bw.write(sb.toString());
br.close();
bw.close();
}
public static class Info {
private String str;
private int val;
public Info(String str, int val) {
this.str = str;
this.val = val;
}
public int getVal() {
return val;
}
public String getStr() {
return str;
}
}
}
반응형
'Algorithm > 문제 풀이' 카테고리의 다른 글
[BAEKJOON_21610 - JAVA] 마법사 상어와 비바라기 (0) | 2023.06.29 |
---|---|
[BAEKJOON_2531 - JAVA] 회전 초밥 (0) | 2023.06.22 |
[BAEKJOON_1446 - JAVA] 지름길 (0) | 2023.06.16 |
[BAEKJOON_14503 -JAVA] 로봇 청소기 (0) | 2023.06.16 |
[BAEKJOON_15723 - JAVA] n단 논법 (0) | 2023.06.16 |