๐ŸŒฑ → ๐ŸŒณ

[๋ฐฑ์ค€] 11000๋ฒˆ : ๊ฐ•์˜์‹ค python ๋ณธ๋ฌธ

Algorithms

[๋ฐฑ์ค€] 11000๋ฒˆ : ๊ฐ•์˜์‹ค python

BAY 2023. 1. 11. 12:28
728x90

https://www.acmicpc.net/problem/11000

 

11000๋ฒˆ: ๊ฐ•์˜์‹ค ๋ฐฐ์ •

์ฒซ ๋ฒˆ์งธ ์ค„์— N์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 200,000) ์ดํ›„ N๊ฐœ์˜ ์ค„์— Si, Ti๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (0 ≤ Si < Ti ≤ 109)

www.acmicpc.net

 

๋ฌธ์ œ ์ •๋ณด

๋ฌธ์ œ ์œ ํ˜• ๋‚œ์ด๋„ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„ ์Šค์Šค๋กœ ๊ตฌํ˜„ ์„ฑ๊ณต

๊ทธ๋ฆฌ๋””, ์šฐ์„ ์ˆœ์œ„ ํ, ์ž๋ฃŒ๊ตฌ์กฐ, ์ •๋ ฌ ๊ณจ๋“œ5 1h X

์„ค๊ณ„ ๋ฐฉ๋ฒ•

๋‚ด๊ฐ€ ์„ค๊ณ„ํ–ˆ๋˜ ๋ฐฉ๋ฒ•

  • input ๋ฐ›์€ ๊ฒƒ์„ ์‹œ์ž‘์‹œ๊ฐ„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
  • ์‹œ์ž‘์‹œ๊ฐ„ S, ๋ ์‹œ๊ฐ„ T list๋ฅผ ๋”ฐ๋กœ ์„ ์–ธ ํ•œ ๋’ค S๋ฅผ ๋Œ๋ฉด์„œ T๊ฐ€ ์ด์ „ ์‹œ์ž‘ ์‹œ๊ฐ„๋“ค ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ์ด์ „ ๊ฐ•์˜์‹ค์„ ๊ณ„์† ์‚ฌ์šฉํ•˜๋„๋ก ํ•จ

์ •๋‹ต ํ’€์ด ๋ฐฉ๋ฒ•

โ‘  ํ˜„์žฌ ํšŒ์˜์‹ค์—์„œ์˜ ํšŒ์˜๊ฐ€ ๋๋‚˜๋Š” ์‹œ๊ฐ„๋ณด๋‹ค ๋‹ค์Œ ํšŒ์˜์˜ ์‹œ์ž‘์‹œ๊ฐ„์ด ๋น ๋ฅด๋ฉด

ํšŒ์˜์‹ค์„ ํ•˜๋‚˜ ์ถ”๊ฐ€๋กœ ๊ฐœ์„คํ•œ๋‹ค.

โ‘ก ํ˜„์žฌ ํšŒ์˜์‹ค์—์„œ ํšŒ์˜๊ฐ€ ๋๋‚˜๋Š” ์‹œ๊ฐ„๋ณด๋‹ค ๋‹ค์Œ ํšŒ์˜์˜ ์‹œ์ž‘์‹œ๊ฐ„์ด ๋Š๋ฆฌ๋ฉด

ํ˜„์žฌ ์กด์žฌํ•˜๋Š” ํšŒ์˜์‹ค์—์„œ ์ด์–ด์„œ ํšŒ์˜ ๊ฐœ์ตœ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

heap์— ์ ค ๋จผ์ € ์‹œ์ž‘ํ•˜๋Š” ๊ฐ•์˜์ข…๋ฃŒ์‹œ๊ฐ„์„ pushํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  1๋ฒˆ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ~ n๋ฒˆ ์ธ๋ฑ์Šค๊นŒ์ง€ํ˜„์žฌ ์ธ๋ฑ์Šค ๊ฐ•์˜ ์‹œ์ž‘์‹œ๊ฐ„์ด heap์—์„œ ๋ฃจํŠธ๋…ธ๋“œ ๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ heap์— ๊ฐ•์˜ ์ข…๋ฃŒ์‹œ๊ฐ„์„ push๋ฅผ ํ•œ๋‹ค. ๊ฐ•์˜ ์‹œ์ž‘ ์‹œ๊ฐ„์ด heap ๋ฃจํŠธ๋…ธ๋“œ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์„ ๊ฒฝ์šฐ์—๋Š” ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•˜๊ณ  ํ˜„์žฌ ์ธ๋ฑ์Šค ๊ฐ•์˜ ์ข…๋ฃŒ์‹œ๊ฐ„์„ pushํ•ด์ค€๋‹ค.

heap์— ์‚ฝ์ž…์„ ํ•˜๋ฉด ์ตœ์†Œํž™์„ ์œ ์ง€๋ฅผ ํ•˜๋ฏ€๋กœ heap์—์„œ 0 ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋Š” ๋ฌด์กฐ๊ฑด ๊ฐ€์žฅ ์ž‘์€๊ฐ’์„ ์œ ์ง€ํ•œ๋‹ค. ์ฆ‰ heap[0]์—๋Š” ์‚ฌ์šฉ์ค‘์ธ ๊ฐ•์˜์‹ค์—์„œ ๊ฐ€์žฅ ๋นจ๋ฆฌ ๋๋‚˜๋Š” ๊ฐ’์ด ์ €์žฅ๋˜์–ด์žˆ๋‹ค. heap[0] ์— ์ €์žฅ๋œ ๊ฐ’์ด ๊ฐ€์žฅ๋นจ๋ฆฌ ๋๋‚˜๋Š” ๊ฐ•์˜์‹ค ์ด๋ฏ€๋กœ ๊ฐ•์˜๊ฐ€ ์‹œ์ž‘ํ•˜๋Š” ์‹œ๊ฐ„์ด ๋น ๋ฅผ ๊ฒฝ์šฐ์—๋Š” heap[1],heap[2].. ์— ์ €์žฅ๋œ ๊ฐ•์˜์‹ค์„ ์ด์šฉํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ์‹œ์ž‘์‹œ๊ฐ„ < heap[0]์ผ ๊ฒฝ์šฐ์—๋Š” ๋ฌด์กฐ๊ฑด ์ƒˆ ๊ฐ•์˜์‹ค์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ heappush๋ฅผ ํ•ด์ค€๋‹ค. heap[0]๋ณด๋‹ค ๊ฐ•์˜ ์‹œ์ž‘ ์‹œ๊ฐ„์ด ํด ๊ฒฝ์šฐ์—๋Š” heap[0] ๊ฐ•์˜์‹ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ heap[0]์„ ์‚ญ์ œํ•ด์ฃผ๊ณ  ํ•ด๋‹น ๊ฐ•์˜ ์ข…๋ฃŒ์‹œ๊ฐ„์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ฐ˜๋ณตํ•œ ๋’ค์— heap์— ์ €์žฅ๋œ ๊ธธ์ด๊ฐ€ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ๊ฐ•์˜์‹ค ๊ฐœ์ˆ˜์ด๋ฏ€๋กœ ์ถœ๋ ฅํ•˜๋ฉด ์ •๋‹ต์ด๋‹ค.

์ฝ”๋“œ

๋‚ด ์ฝ”๋“œ(์‹œ๊ฐ„์ดˆ๊ณผ ๋ฐœ์ƒ)

import sys
input = sys.stdin.readline

n = int(input())
cnt = 0 
temp =[]
for _ in range(n):
  time = list(map(int, input().split()))
  temp.append(time)

# time[0] ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ 
# temp = sorted(temp, key=lambda x: x[0])
temp.sort(key=lambda x:x[0])

S = []
T = []

for i in temp:
  S.append(i[0])
  T.append(i[1])

for i in range(len(S)):
  if i == 0:
    cnt += 1
  for j in range(i):
    if T[j] <= S[i]:
      cnt += 1

print(cnt)

์ •๋‹ต ์ฝ”๋“œ

import sys
import heapq

heap = []
input = sys.stdin.readline
n = int(input())
arr = []
for i in range(n):
    a, b = map(int, input().split())
    arr.append([a, b])
arr.sort(key=lambda x: x[0])
heapq.heappush(heap, arr[0][1])  #์ฒซ๋ฒˆ์งธ ๊ฐ•์˜๊ฐ€ ๋๋‚˜๋Š” ์‹œ๊ฐ„์„ ๋„ฃ์Œ
for i in range(1, n):
    if heap[0] > arr[i][0]:
        heapq.heappush(heap, arr[i][1])
    else:
        heapq.heappop(heap)
        heapq.heappush(heap, arr[i][1])
print(len(heap))

์‹œ๊ฐ„ ๋ณต์žก๋„

$O(NlogN)$

heapq.heappush(pop)์ด nlogn์˜ ์‹œ๊ฐ„๋ณต์žก๋„ ๊ฐ€์ง

์–ด๋ ค์› ๋˜ ์ 

๋‚ด๊ฐ€ ์ฒ˜์Œ์— ์ƒ๊ฐํ–ˆ๋˜ ๋Œ€๋กœ ์ฝ”๋“œ๋ฅผ ์งœ๋ฉด for๋ฌธ์ด 2๋ฒˆ ์ค‘์ฒฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚  ์ˆ˜ ๋ฐ–์— ์—†๋‹ค

  • ์šฐ์„ ์ˆœ์œ„ ํ๋‚˜ ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•ด ๋” ๊นŠ์ด ๊ณต๋ถ€ ํ•„์š”
  • time[0] ๊ธฐ์ค€์œผ๋กœ sortํ•˜๊ณ  ์‹ถ์œผ๋ฉด ๊ทธ๋ƒฅ time.sort()ํ•ด๋„ ๋˜๋Š” ๋“ฏ ์ด๋Ÿฐ ๊ธฐ์ดˆ์ ์ธ ๊ฒƒ๋„ ๋” ๊ผผ๊ผผํ•˜๊ฒŒ ๊ณต๋ถ€ํ•  ๊ฒƒ
  • ์ฒ˜์Œ์— ๋ฌธ์ œ๋ฅผ ์ ‘ํ–ˆ์„ ๋•Œ ๊ผญ ์‹œ๊ฐ„๋ณต์žก๋„ ์ƒ๊ฐํ•˜๊ธฐ ๋ฐ˜๋ณต๋ฌธ์„ ๋‘ ๋ฒˆ ์ด์ƒ ์ค‘์ฒฉ์‹œํ‚ค๋Š” ํ’€์ด๋Š” ์ตœ๋Œ€ํ•œ ํ”ผํ•˜๊ธฐ

์ฐธ๊ณ  ์ž๋ฃŒ

https://velog.io/@slbin-park/๋ฐฑ์ค€-11000๋ฒˆ-๊ฐ•์˜์‹ค-๋ฐฐ์ •-ํŒŒ์ด์ฌ

[๋ฐฑ์ค€] 11000 ๊ฐ•์˜์‹ค ๋ฐฐ์ • (Python ํŒŒ์ด์ฌ)

728x90