본문 바로가기
백준

[백준] 18870번 파이썬 (좌표 압축)

by 헤이즐넛 좋아하는 개발자 2024. 2. 11.

시간초과 오류가 발생해서 다른 사람들의 해설을 찾아 해결했다.

import sys

input = sys.stdin.readline
n = int(input())

# Ex) lst = [2, 4, -10, 4, -9]
lst = list(map(int, input().split()))

# Ex) sort_lst = [-10, -9, 2, 4]
sort_lst = sorted(list(set(lst)))

# Ex) num_dict = {-10:0, -9:1, 2:2, 4:3]
num_dict = {sort_lst[i] : i for i in range(len(sort_lst))}

# Ex) i == 2(lst[0])일 때, num_dict[i] == 2
# Ex) i == 4(lst[1])일 때, num_dict[i] == 3
for i in lst:
    sys.stdout.write("{} ". format(str(num_dict[i])))