Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 인턴 지원
- EER
- restapi
- jsp
- webhacking 처음
- 동읍면 DB
- react
- 행정지역 DB
- riceteacatpanda
- 메모리 포랜식
- 소개딩
- Forensic 절차
- spring
- reversing.kr
- mysql
- PyAmdecoder
- DBMS
- frontend
- ㅁㅇㅂ??ㅇㅈㄷ ㅎㅇㅌ...
- 3단계 지역 DB
- Database
- Layered Architecture
- 인턴 후기
- 방명록 만들기
- JSTL
- SessionAttribute
- 네이버 인턴
- 소프트웨어 개발보안 경진대회
- Django
- 정보보호병 후기
Archives
- Today
- Total
웹찢남
[백준 1927 최소 힙] PYTHON 본문
문제 이름도 최소 힙이고 쓰라고 써있으니 python 최소 힙이라 검색을 해보자
heapq라는 모듈이 있다. 갖다 쓰자
아래처럼 간단하게 짤 수 있다.
혹시 모르시는 분들을 위해 heapq에 대해 설명을 하면
heapq.heappop(Q)는 최소 값을 pop하고 반환한다.
heapq.heappop(Q,i)는 i를 heapq에 넣는 것이다.
참고로 최소 힙의 정의는 부모 노드의 키값이 자식 노드의 키값보다 항상 작은 힙이다.
+ sys.stdin.readline()은 input 값이 많을 때 유용하다 (시간적으로)
import heapq
import sys
a=int(input())
heap=[]
for _ in range(a):
i = int(sys.stdin.readline())
if(i==0):
try:
print(heapq.heappop(heap))
except:
print(0)
else:
heapq.heappush(heap,i)
'백준 Algorithm' 카테고리의 다른 글
[백준 1260 DFS와 BFS] PYTHON (0) | 2021.02.26 |
---|---|
[백준 1916 최소비용 구하기] PYTHON (0) | 2021.02.26 |
[백준 1463 1로 만들기] PYTHON (0) | 2021.02.26 |
[백준 11047 동전 0] PYTHON (0) | 2021.02.26 |
[백준 11399 ATM] PYTHON (0) | 2021.02.26 |
Comments