[백준] 11286, 11650 - Python3

shsh·2021년 12월 17일

백준

목록 보기
36/45

11650. 좌표 정렬하기

https://www.acmicpc.net/problem/11650

내 풀이 - 성공

from sys import stdin

N = int(stdin.readline())
coords = []

for _ in range(N):
    x, y = map(int, stdin.readline().split())
    coords.append((x, y))

coords.sort(key = lambda x: (x[0], x[1]))

for x, y in coords:
    print(x, y)

입력받은 좌표들을 sort() 의 lambda 식을 이용

x 좌표 -> y 좌표 의 우선순위로 정렬


11286. 절댓값 힙

https://www.acmicpc.net/problem/11286

내 풀이 - 성공

from sys import stdin
import heapq

N = int(stdin.readline())
heap = []

for _ in range(N):
    x = int(stdin.readline())
    
    if x == 0:
        if heap:
            h = heapq.heappop(heap)
            print(h[1])
        else:
            print(0)
    else:
        heapq.heappush(heap, (abs(x), x))

최대힙을 저장하듯이 (절댓값, 값) 으로 묶어서 저장
=> 절댓값을 기준으로 정렬

출력할 때는 원래 값을 출력 => h[1]

profile
Hello, World!

0개의 댓글