🌞Algorithm/πŸ”₯Baekjoon

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

λΏŒμ•Ό._. 2023. 8. 15. 12:22

Gold III

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

< κ°•μ˜μ‹€ 2 >

 

문제 풀이 

 

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

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

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

 

a의 값을 μˆœμ„œλŒ€λ‘œ b에 μ €μž₯ν•˜λŠ”λ°, b의 μ € μ•žμ— κ°’μ˜ κ°•μ˜ λλ‚˜λŠ” μ‹œκ°„λ³΄λ‹€ aμ—μ„œμ˜ κ°•μ˜ μ‹œμž‘ μ‹œκ°„μ΄ κ°™κ±°λ‚˜ 크닀면 b의 맨 μ•žμ˜ κ°•μ˜κ°€ λλ‚œ 후에 κ·Έ κ°•μ˜μ‹€μ„ μ‚¬μš©ν•  수 μžˆλ‹€λŠ” λœ»μ΄λ―€λ‘œ b의 값을 poll ν•œ ν›„ μ €μž₯ν•΄ μ€€λ‹€.

 

μ΄λ•Œ, κ·Έ κ°•μ˜ 번호λ₯Ό index둜 ν•˜λŠ” 배열에 κ°•μ˜μ‹€ 번호λ₯Ό μ €μž₯ν•΄ μ€€λ‹€.

 

 

 my solution (Java)

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

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

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

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

		int result[] = new int[n + 1];

		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());
			int num = Integer.parseInt(st.nextToken());
			queue.add(new int[] { Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), num });
		}

		PriorityQueue<int[]> room = 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];
			}
		});

		int lecture[] = queue.poll();
		room.add(new int[] { lecture[1], 1 });
		result[lecture[2]] = 1;

		int idx = -1;
		while (!queue.isEmpty()) {
			lecture = queue.poll();
			idx = -1;
			if (room.peek()[0] <= lecture[0]) {
				int temp[] = room.poll();
				idx = temp[1];
			}
			if (idx == -1) {
				idx = room.size() + 1;
			}
			result[lecture[2]]=idx;
			room.add(new int[] { lecture[1], idx });
		}

		bw.write(room.size() + "\n");
		for (int i = 1; i <= n; i++) {
			bw.write(result[i] + "\n");
		}
		bw.flush();
	}
}

 

Main

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

 

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

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

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

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

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

: poll ν–ˆλ‹€λ©΄ κ·Έ κ°•μ˜μ‹€μ— λ“€μ–΄κ°€μ•Ό ν•˜λ―€λ‘œ κ°•μ˜μ‹€ 번호둜 μ €μž₯ But μƒˆλ‘œμš΄ κ°•μ˜μ‹€μ΄λΌλ©΄ κ°•μ˜μ‹€ 번호둜 room의 size+1을 μ €μž₯

- κ°•μ˜μ‹€ κ°œμˆ˜μ™€ 각 κ°•μ˜μ— λ°°μ •ν•  κ°•μ˜μ‹€ 번호λ₯Ό μˆœμ„œλŒ€λ‘œ 좜λ ₯