[백준] 2012번: 등수 매기기

whitehousechef·2023년 10월 23일
0

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

initial

The question is quite simple though at first attempt, my code was time inefficient. We sort the given list and iterate and compare with the ascending order of ranks (i.e. the index of list from 1 to n), the difference is abs(lst[i]-i)

we can just iterate by the index but i created a tmp list from 1 to n and compared its element with the given lst's element. efficient!!

revisited jan 26th

yes straightforward greedy q

solution

import sys
input = sys.stdin.readline

n = int(input())
lst = [0]
for _ in range(n):
    lst.append(int(input()))
lst.sort()
ans = 0
for i in range(1, n + 1):
    ans += abs(lst[i] - i)
print(ans)

complexity

n time and space

0개의 댓글