πŸ—οΈ Algorithm/🟩 λ°±μ€€

🟩 [λ°±μ€€] [Python] [Class3] 18870번_μ’Œν‘œ μ••μΆ•

Dbswnstjd 2022. 11. 7. 01:41

문제

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

 

18870번: μ’Œν‘œ μ••μΆ•

μˆ˜μ§μ„  μœ„μ— N개의 μ’Œν‘œ X1, X2, ..., XN이 μžˆλ‹€. 이 μ’Œν‘œμ— μ’Œν‘œ 압좕을 μ μš©ν•˜λ €κ³  ν•œλ‹€. Xiλ₯Ό μ’Œν‘œ μ••μΆ•ν•œ κ²°κ³Ό X'i의 값은 Xi > Xjλ₯Ό λ§Œμ‘±ν•˜λŠ” μ„œλ‘œ λ‹€λ₯Έ μ’Œν‘œμ˜ κ°œμˆ˜μ™€ κ°™μ•„μ•Ό ν•œλ‹€. X1, X2, ..., XN에 쒌

www.acmicpc.net

풀이

# λ°±μ€€ 18870번 문제 - μ’Œν‘œ μ••μΆ•
n = int(input())
x = list(map(int, input().split()))
new_x = list(sorted(set(x)))

dict = {new_x[i]: i for i in range(len(new_x))}

for i in x:
    print(dict[i], end=" ")

맨 처음 μ‹œκ°„μ΄ˆκ³Όκ°€ λ– μ„œ μ‹œκ°„λ³΅μž‘λ„λ₯Ό μƒκ°ν•˜μ˜€λ‹€. 

Nμ΄λΌλŠ” μˆ«μžκ°€ 100만이기 λ•Œλ¬Έμ— μ‹œκ°„λ³΅μž‘λ„λ₯Ό λ¨Όμ € μƒκ°ν–ˆμ–΄μ•Ό ν–ˆλŠ”λ° ν•˜μ§€ λͺ»ν–ˆλ‹€. 

index(i)λŠ” μ‹œκ°„λ³΅μž‘λ„κ°€ O(n)이고 λ”•μ…”λ„ˆλ¦¬λŠ” O(1) μ΄λ―€λ‘œ λ”•μ…”λ„ˆλ¦¬λ₯Ό μ‚¬μš©ν•΄μ•Ό ν–ˆλ‹€.