본문 바로가기

알고리즘 문제풀이

연료 채우기 [백준 1826]

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

 

import heapq
import sys
# input = sys.stdin.read
# data = input().splitlines()

N = int(input())  # 주유소 개수
stations = []
for i in range(1, N + 1):
    # a, b = map(int, data[i].split())
    a, b = map(int, input().split())
    stations.append((a, b))
L, P = map(int, input().split())  # 마을까지 거리, 현재 연료
stations.sort()
max_heap = []
fuel = P
count = 0
index = 0  
while fuel < L:
    while index < N and stations[index][0] <= fuel:
        heapq.heappush(max_heap, -stations[index][1])
        index += 1
    if not max_heap:
        print(-1)
        exit()
    fuel += -heapq.heappop(max_heap)
    count += 1  # 주유 횟수 증가


print(count)