🌞Algorithm/πŸ”₯Baekjoon

[Baekjoon] 1374_κ°•μ˜μ‹€

λΏŒμ•Ό._. 2023. 8. 15. 11:16

Gold V

문제(좜처: https://www.acmicpc.net/problem/1374)

< κ°•μ˜μ‹€ >

 

문제 풀이 

 

μš°μ„ μˆœμœ„ 큐 2개λ₯Ό μ‚¬μš©ν•΄μ„œ 문제λ₯Ό ν•΄κ²°ν–ˆλ‹€.

μš°μ„ μˆœμœ„ 큐 ν•˜λ‚˜μ—λŠ” [κ°•μ˜ μ‹œμž‘ μ‹œκ°„, κ°•μ˜ λλ‚˜λŠ” μ‹œκ°„] 배열을 μ €μž₯ν•΄μ„œ μš°μ„ μˆœμœ„λ₯Ό κ°•μ˜ μ‹œμž‘μ‹œκ°„μ΄ λΉ λ₯Έ 순으둜, μ‹œμž‘ μ‹œκ°„μ΄ κ°™λ‹€λ©΄ λλ‚˜λŠ” μ‹œκ°„μ΄ λΉ λ₯Έ 순으둜 μ •λ ¬ν–ˆλ‹€.

λ‹€λ₯Έ μš°μ„ μˆœμœ„ νμ—λŠ” κ°•μ˜ λλ‚˜λŠ” μ‹œκ°„μ„ μ €μž₯ν•œλ‹€. μ΄λ•Œ, 이 μš°μ„ μˆœμœ„ 큐 μ € μ•žμ— μžˆλŠ” 값이 μƒˆλ‘œ λ“€μ–΄μ˜€λŠ” κ°’ 보닀 μž‘κ±°λ‚˜ κ°™λ‹€λ©΄ κ°•μ˜κ°€ λλ‚œ ν›„ λ‹€μŒ 회의λ₯Ό μ‹œμž‘ν•  수 μžˆλŠ” κ²ƒμ΄λ―€λ‘œ pollν•΄μ€€ ν›„ λ„£μ–΄μ€€λ‹€.

 

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class _1374_ { // κ°•μ˜μ‹€

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		int n = Integer.parseInt(bf.readLine());

		PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {

			@Override
			public int compare(int[] o1, int[] o2) {
				if (o1[0] == o2[0])
					return o1[1] - o2[1];
				return o1[0] - o2[0];
			}
		});

		for (int i = 0; i < n; i++) {
			st = new StringTokenizer(bf.readLine());
			st.nextToken();
			queue.add(new int[] { Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()) });
		}

		PriorityQueue<Integer> room = new PriorityQueue<>();
		room.add(queue.poll()[1]);

		while (!queue.isEmpty()) {
			int time[] = queue.poll();
			if (room.peek() <= time[0]) {
				room.poll();
			}
			room.add(time[1]);
		}
		System.out.println(room.size());
	}
}

 

Main

λ³€μˆ˜)
n : κ°•μ˜ 수
queue: μš°μ„ μˆœμœ„ 큐 [κ°•μ˜ μ‹œμž‘ μ‹œκ°„, κ°•μ˜ λλ‚œ μ‹œκ°„]
room: μš°μ„ μˆœμœ„ 큐 [κ°•μ˜ λλ‚œ μ‹œκ°„] 

 

- κ°•μ˜ 수(n) μž…λ ₯

- κ°•μ˜ 수만큼 μž…λ ₯λ°›μ•„ μš°μ„ μˆœμœ„ 큐 queue에 μ €μž₯

- room μš°μ„ μˆœμœ„ 큐에 queue 값을 뽑아 λ¨Όμ € κ°•μ˜μ‹€ ν•˜λ‚˜ μ €μž₯

- queue κ°€ 빌 λ•ŒκΉŒμ§€ 반볡

: room μš°μ„ μˆœμœ„ 큐의 μ € 첫 번째 값이 λ“€μ–΄κ°ˆ κ°’ 보닀 μž‘κ±°λ‚˜ κ°™λ‹€λ©΄ κ°•μ˜κ°€ λλ‚œ ν›„ λ‹€μŒ κ°•μ˜λ₯Ό μ‹œμž‘ν•  수 μžˆλ‹€λŠ” λœ»μ΄λ―€λ‘œ poll ν›„ μΆ”κ°€

- κ°•μ˜μ‹€ κ°œμˆ˜λŠ” room의 sizeμ΄λ―€λ‘œ room size 좜λ ₯