[백준/BOJ] 1826번 연료 채우기
1826번: 연료 채우기
첫째 줄에 주유소의 개수 N(1 ≤ N ≤ 10,000)가 주어지고 두 번째 줄부터 N+1번째 줄 까지 주유소의 정보가 주어진다. 주유소의 정보는 두개의 정수 a,b로 이루어 져 있는데 a(1 ≤ a ≤ 1,000,000)는 성경
www.acmicpc.net
이전에 풀었던 보석 도둑과 유사하여 비슷한 알고리즘으로 풀었다.
Python code
더보기
import sys
import heapq
n=int(sys.stdin.readline())
gas=[]
for _ in range(n):
heapq.heappush(gas,list(map(int, sys.stdin.readline().split())))
l,p=map(int, sys.stdin.readline().split())
capable=[]
cnt=0
while p<l:
while gas and gas[0][0]<=p:
heapq.heappush(capable, -heapq.heappop(gas)[1])
if not capable:
cnt=-1
break
p+=-heapq.heappop(capable)
cnt+=1
print(cnt)
'baekjoon' 카테고리의 다른 글
[백준/BOJ] 1422번 숫자의 신 (Python) (0) | 2021.03.14 |
---|---|
[백준/BOJ] 9576번 책 나눠주기 (Python) (0) | 2021.03.13 |
[백준/BOJ] 13305번 주유소 (Python) (0) | 2021.03.12 |
[백준/BOJ] 11286번 절댓값 힙 (Python) (0) | 2021.03.12 |
[백준/BOJ] 11279번 최대 힙 (Python) (0) | 2021.03.12 |