🌞Algorithm/πŸ”₯Baekjoon

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

λΏŒμ•Ό._. 2023. 8. 14. 14:51

Gold V

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

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

 

문제 풀이 

 

μš°μ„ μˆœμœ„ 큐 a에 [μˆ˜μ—… μ‹œμž‘ μ‹œκ°„, λλ‚œ μ‹œκ°„] λ°°μ—΄λ‘œ μ €μž₯ν•œλ‹€. μ΄λ•Œ μš°μ„ μˆœμœ„λŠ” μˆ˜μ—… μ‹œμž‘μ‹œκ°„μ΄ λΉ λ₯Έ 순으둜, μ‹œμž‘ μ‹œκ°„μ΄ κ°™λ‹€λ©΄ λλ‚˜λŠ” μ‹œκ°„μ΄ λΉ λ₯Έ 순으둜 μ •λ ¬ν•œλ‹€. μš°μ„ μˆœμœ„ 큐 a μ•žμ— λ“€μ–΄μžˆλŠ” κ°’λΆ€ν„° 뽑아내어 또 λ‹€λ₯Έ μš°μ„ μˆœμœ„ 큐 b에 λλ‚˜λŠ” μ‹œκ°„λ§Œ μ €μž₯ν•œλ‹€. μ΄λ•Œ, b μ € μ•žμ— λ“€μ–΄μžˆλŠ” 값이 μ €μž₯ν•˜λ €λŠ” κ°’ 보닀 μž‘κ±°λ‚˜ κ°™λ‹€λ©΄ μˆ˜μ—…μ΄ λλ‚œ ν›„ λ‹€μŒ μˆ˜μ—…μ„ μ‹œμž‘ν•  수 μžˆμœΌλ―€λ‘œ bμ—μ„œ 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 _11000_ { // κ°•μ˜μ‹€ λ°°μ •

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

		PriorityQueue<Integer> temp = new PriorityQueue<>();

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

 

Main

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

 

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

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

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

: tempκ°€ λΉ„μ–΄μžˆλ‹€λ©΄ κ·Έλƒ₯ μ €μž₯

: tempκ°€ λΉ„μ–΄μžˆμ§€ μ•Šκ³  μ € 첫 번째 값이 λ“€μ–΄κ°ˆ κ°’ 보닀 μž‘κ±°λ‚˜ κ°™λ‹€λ©΄ μˆ˜μ—…μ΄ λλ‚œ ν›„ λ‹€μŒ μˆ˜μ—…μ„ μ‹œμž‘ν•  수 μžˆλ‹€λŠ” λœ»μ΄λ―€λ‘œ poll ν›„ μΆ”κ°€

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