목차

연료 채우기

ps
링크acmicpc.net/…
출처BOJ
문제 번호1826
문제명연료 채우기
레벨골드 2
분류

그리디

시간복잡도O(nlogn)
인풋사이즈n<=10000
사용한 언어Python
제출기록32928KB / 92ms
최고기록56ms
해결날짜2022/02/23

풀이

코드

"""Solution code for "BOJ 1826. 연료 채우기".

- Problem link: https://www.acmicpc.net/problem/1826
- Solution link: http://www.teferi.net/ps/problems/boj/1826

Tags: [Greedy]
"""

import heapq
import sys

INF = float('inf')


def main():
    N = int(sys.stdin.readline())
    stations = []
    for _ in range(N):
        a, b = [int(x) for x in sys.stdin.readline().split()]
        stations.append((a, b))
    L, P = [int(x) for x in sys.stdin.readline().split()]

    heap = []
    answer = 0
    movable_dist = P
    stations.append((L, INF))
    for dist, fuel in sorted(stations):
        while heap and movable_dist < dist:
            movable_dist += -heapq.heappop(heap)
            answer += 1
        if movable_dist < dist:
            print('-1')
            break
        heapq.heappush(heap, -fuel)
    else:
        print(answer)


if __name__ == '__main__':
    main()