일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- recoil
- useAppDispatch
- CI/CD
- 무한 스크롤
- async/await
- 결정 알고리즘
- app router
- 공변성
- Promise
- React
- TS
- tailwind
- map
- ESlint
- Cypress
- webpack
- autosize
- 태그된 유니온
- dfs
- 호이스팅
- 이분 검색
- 인터섹션
- 반공변성
- 투포인터
- SSR
- 리터럴 타입
- RTK Query
- Jest
- CORS
- 타입 좁히기
Archives
- Today
- Total
짧은코딩
7662 이중 우선순위 큐 본문
반응형
https://www.acmicpc.net/problem/7662
7662번: 이중 우선순위 큐
입력 데이터는 표준입력을 사용한다. 입력은 T개의 테스트 데이터로 구성된다. 입력의 첫 번째 줄에는 입력 데이터의 수를 나타내는 정수 T가 주어진다. 각 테스트 데이터의 첫째 줄에는 Q에 적
www.acmicpc.net
내 풀이(구글링해서 찾아보고 안보고 풀음)
import heapq
t = int(input())
rst = []
for i in range(t):
n = int(input())
chk = [False]*1_000_001
max = []
min = []
for j in range(n):
x = list(input().split())
if x[0] == 'I':
heapq.heappush(max, (-int(x[1]), j))
heapq.heappush(min, (int(x[1]), j))
chk[j] = True
else:
if x[1] == '1':
while max and not chk[max[0][1]]: heapq.heappop(max)
if max:
chk[max[0][1]] = False
heapq.heappop(max)
else:
while min and not chk[min[0][1]]: heapq.heappop(min)
if min:
chk[min[0][1]] = False
heapq.heappop(min)
while max and not chk[max[0][1]]: heapq.heappop(max)
while min and not chk[min[0][1]]: heapq.heappop(min)
rst.append([])
if max and min:
m = max[0][0]
rst[i].append(-max[0][0])
rst[i].append(min[0][0])
else:
rst[i].append("EMPTY")
for i in rst:
if len(i) == 1:
print(i[0])
else:
print('{} {}'.format(i[0], i[1]))
max는 최대힙, min은 최소힙이다. chk는 각 번호마다 id를 부여해 false면 삭제한다. 최대힙은 -를 붙여서 넣어서 구현했다.
while max and not chk[max[0][1]]: heapq.heappop(max)
이런 부분에서는 힙이 있고 chk의 id가 false면 삭제하라는 의미이다. 그리고 마지막에 또 해줘서 false인 것을 완전히 삭제한다.
그리고 format 함수를 사용하여 결과값을 출력했다.
반응형
'코딩 테스트(Python) > 백준, 프로그래머스' 카테고리의 다른 글
프로그래머스) 기능개발 (0) | 2022.05.01 |
---|---|
1766 문제집 (0) | 2022.04.15 |
프로그래머스) 완주하지 못한 선수 (0) | 2022.04.06 |
프로그래머스) 숫자 문자열과 영단어 (0) | 2022.03.29 |
프로그래머스) 오픈채팅방 (0) | 2022.03.29 |
Comments