๐™ƒ๐™š๐™–๐™ฅ - ์ค‘๊ฐ„๊ฐ’ ๊ตฌํ•˜๊ธฐ

uuuouuoยท2022๋…„ 7์›” 28์ผ
0
post-thumbnail

Priority Queue๋ฅผ ํ†ตํ•œ ์ค‘๊ฐ„๊ฐ’ ๊ตฌํ•˜๊ธฐ


  • ์‚ฝ์ž…ํ•  ๋•Œ๋งˆ๋‹ค ์ค‘์•™๊ฐ’ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ
  • ํšจ์œจ์ ์œผ๋กœ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด Heap์œผ๋กœ ๊ตฌ์„ฑ๋œ Priority Queue ์ด์šฉ
  • ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(nlogn)

๊ตฌํ˜„ ๊ทœ์น™


  1. ์ œ์ผ ์ฒ˜์Œ ์ตœ๋Œ€ ํž™์— ์‚ฝ์ž…
  2. ์ตœ๋Œ€ ํž™์˜ ํฌ๊ธฐ๋Š” ์ตœ์†Œ ํž™์˜ ํฌ๊ธฐ์™€ ๊ฐ™๊ฑฐ๋‚˜, ํ•˜๋‚˜ ๋” ํผ
  3. ์ตœ๋Œ€ ํž™์˜ ์ตœ๋Œ€ ์›์†Œ๋Š” ์ตœ์†Œ ํž™์˜ ์ตœ์†Œ ์›์†Œ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์Œ
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋งž์ง€ ์•Š๋‹ค๋ฉด ์ตœ๋Œ€ ํž™, ์ตœ์†Œ ํž™์˜ ๊ฐ€์žฅ ์œ„์˜ ๊ฐ’ swap
  1. ์ด ๋‘๊ฐ€์ง€ ๊ทœ์น™์„ ์œ ์ง€ํ•ด ์ค€๋‹ค๋ฉด ํ•ญ์ƒ ์ตœ๋Œ€ ํž™ top๊ฐ’์ด ์ค‘๊ฐ„๊ฐ’

SWEA ์ค‘๊ฐ„๊ฐ’ ๊ตฌํ•˜๊ธฐ

  • ๋งจ ์ฒ˜์Œ 5 ์‚ฝ์ž…
  • ๊ทธ ํ›„ 1, 3, 2, 6, 8, 9 ์ฐจ๋ก€๋Œ€๋กœ ์‚ฝ์ž…

์ œ์ผ ์ฒ˜์Œ ์ตœ๋Œ€ ํž™์— ์‚ฝ์ž…

  • ์ตœ๋Œ€ ํž™์˜ ํฌ๊ธฐ๊ฐ€ ๋” ์ปค์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ (1๋ฒˆ ๊ทœ์น™)
  max heap				min heap
  
	(5)

๋‹ค์Œ ์ˆซ์ž ์‚ฝ์ž… (์ตœ์†Œ, ์ตœ๋Œ€ ์ˆœ์œผ๋กœ)

  • 1, 3 ์‚ฝ์ž…
  max heap				min heap
  
	(5)					(1)
    /
  (3)

์ตœ๋Œ€ ํž™ ๋ฃจํŠธ๋…ธ๋“œ์™€ ์ตœ์†Œ ํž™ ๋ฃจํŠธ๋…ธ๋“œ ๋น„๊ต

  • ์ตœ๋Œ€ ํž™์˜ ๋ฃจํŠธ๋…ธ๋“œ๊ฐ€ ๋” ์ž‘์•„์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— swap (2๋ฒˆ ๊ทœ์น™)
  • ์ด ๋•Œ ์ค‘๊ฐ„๊ฐ’ : 3
  max heap				min heap
  
	(3)					(5)
    /
  (1)

๋‹ค์Œ ์ˆซ์ž ์‚ฝ์ž… (์ตœ์†Œ, ์ตœ๋Œ€ ์ˆœ์œผ๋กœ)

  • 2, 6 ์‚ฝ์ž…
  max heap				min heap
  
	(6)					(2)
    / \					/
  (3) (1)			  (5)

์ตœ๋Œ€ ํž™ ๋ฃจํŠธ๋…ธ๋“œ์™€ ์ตœ์†Œ ํž™ ๋ฃจํŠธ๋…ธ๋“œ ๋น„๊ต

  • ์ตœ๋Œ€ ํž™์˜ ๋ฃจํŠธ๋…ธ๋“œ๊ฐ€ ๋” ์ž‘์•„์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— swap (2๋ฒˆ ๊ทœ์น™)
  • ์ด ๋•Œ ์ค‘๊ฐ„๊ฐ’ : 3
  max heap				min heap
  
	(3)					(5)
    / \					/
  (2) (1)			  (6)

๋‹ค์Œ ์ˆซ์ž ์‚ฝ์ž… (์ตœ์†Œ, ์ตœ๋Œ€ ์ˆœ์œผ๋กœ)

  • 8, 9 ์‚ฝ์ž…
  max heap				min heap
  
	(9)					(5)
    / \					/ \
  (3) (2)			  (6) (8)
  /
(1)

์ตœ๋Œ€ ํž™ ๋ฃจํŠธ๋…ธ๋“œ์™€ ์ตœ์†Œ ํž™ ๋ฃจํŠธ๋…ธ๋“œ ๋น„๊ต

  • ์ตœ๋Œ€ ํž™์˜ ๋ฃจํŠธ๋…ธ๋“œ๊ฐ€ ๋” ์ž‘์•„์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— swap (2๋ฒˆ ๊ทœ์น™)
  • ์ด ๋•Œ ์ค‘๊ฐ„๊ฐ’ : 5
  max heap				min heap
  
	(5)					(6)
    / \					/ \
  (3) (2)			  (8) (9)
  /
(1)

๊ตฌํ˜„ ์•Œ๊ณ ๋ฆฌ์ฆ˜

import java.io.*;
import java.util.*;

public class ์ค‘๊ฐ„๊ฐ’๊ตฌํ•˜๊ธฐ {

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    StringTokenizer st;
		StringBuilder sb = new StringBuilder();
		
		int T = Integer.parseInt(br.readLine());
		for(int t = 1; t <= T; t++) {
			sb.append("#"+t+" ");
			
			st = new StringTokenizer(br.readLine());
			int N = Integer.parseInt(st.nextToken());
			PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
			PriorityQueue<Integer> minHeap = new PriorityQueue<>();
			
			maxHeap.add(Integer.parseInt(st.nextToken()));
			
			long sum = 0;
			for (int i = 0; i < N; i++) {
				st = new StringTokenizer(br.readLine());
				minHeap.add(Integer.parseInt(st.nextToken()));
				maxHeap.add(Integer.parseInt(st.nextToken()));
				
				if(minHeap.peek() < maxHeap.peek()) {
					int min = minHeap.poll(), max = maxHeap.poll();
					minHeap.add(max);
					maxHeap.add(min);
				}				
				sum += maxHeap.peek();
			}
			
			sb.append(sum%20171109 + "\n");
		}
		System.out.println(sb);

	}

}

0๊ฐœ์˜ ๋Œ“๊ธ€