File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #2023-04-15-Week2-과제
2+ #11279_최대 힙
3+
4+ '''
5+ 배열에 자연수 x를 넣는다.
6+ 배열에서 가장 큰 값을 출력하고, 그 값을 배열에서 제거한다.
7+ 프로그램은 처음에 비어있는 배열에서 시작하게 된다.
8+
9+ 첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다.
10+ 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가
11+ 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고,
12+ x가 0이라면 배열에서 가장 큰 값을 출력하고
13+ 그 값을 배열에서 제거하는 경우이다.
14+ 입력되는 자연수는 231보다 작다.
15+
16+ 입력에서 0이 주어진 회수만큼 답을 출력한다.
17+ 만약 배열이 비어 있는 경우인데
18+ 가장 큰 값을 출력하라고 한 경우에는 0을 출력하면 된다.
19+ '''
20+
21+
22+ '''
23+ import heapq
24+ heap = []
25+ num = int(input())
26+
27+ for i in range(num) :
28+ a = int(input())
29+ if a==0:
30+ if heap :
31+ print(heapq.heappop(heap)[1])
32+ else :
33+ print(0)
34+ elif a>0 :
35+ heapq.heappush(heap, (-a,a))
36+ '''
37+
38+ #채점 결과 시간 초과
39+ #input() -> sys.stdin.readline()
40+
41+ import heapq
42+ import sys
43+ heap = []
44+ num = int (sys .stdin .readline ())
45+
46+ for i in range (num ) :
47+ a = int (sys .stdin .readline ())
48+ if a == 0 : #a가 0이라면
49+ if heap :
50+ print (heapq .heappop (heap )[1 ]) # 배열에서 가장 큰 값을 출력하고 그 값을 배열에서 제거하는 경우이다.
51+ else : #
52+ print (0 )
53+ elif a > 0 : #a가 자연수라면
54+ heapq .heappush (heap , (- a ,a )) #a라는 값을 넣는 연산
55+
You can’t perform that action at this time.
0 commit comments