240514 IF문 좀 대신 써줘

Jongleee·2024년 5월 14일
0

TIL

목록 보기
572/737
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringBuilder sb = new StringBuilder();
static String[] jobs;
static int[] powers;

public static void main(String[] args) throws Exception {
	StringTokenizer st = new StringTokenizer(br.readLine());
	int size = Integer.parseInt(st.nextToken());
	int human = Integer.parseInt(st.nextToken());

	jobs = new String[size];
	powers = new int[size];

	for (int i = 0; i < size; i++) {
		st = new StringTokenizer(br.readLine());
		jobs[i] = st.nextToken();
		powers[i] = Integer.parseInt(st.nextToken());
	}

	for (int i = 0; i < human; i++) {
		int power = Integer.parseInt(br.readLine());
		int resultIndex = findClosestJobIndex(power);
		sb.append(jobs[resultIndex]).append("\n");
	}

	System.out.print(sb);
}

static int findClosestJobIndex(int target) {
	int left = 0;
	int right = powers.length - 1;

	while (left < right) {
		int mid = (left + right) / 2;
		if (powers[mid] < target) {
			left = mid + 1;
		} else {
			right = mid;
		}
	}

	return left;
}

출처:https://www.acmicpc.net/problem/19637

0개의 댓글