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()