*문제 출처는 백준에 있습니다.
문제 제목: 등수 매기기 / 2012번 (실버 3단계)
문제 사이트: https://www.acmicpc.net/problem/2012
문제 설명

나의 풀이
import sys
# 탐욕법으로 오름차순으로 정렬 후 자기 등수에서 예상 등수를 빼면 가장 적게 나올 것 이다.
def solution(student):
answer = 0
for i in range(len(student)):
current = (i + 1) - student[i] # (|A - B|)
if current < 0:
answer -= current
else:
answer += current
return answer
def main():
n = int(input()) # 사람 수
student = sorted([int(sys.stdin.readline().strip()) for _ in range(n)]) # 학생
print(solution(student))
if __name__=="__main__":
main()

※ 알아야 할 것
탐욕으로 오름차순으로 정렬 후 자기 등수에서 예상 등수를 빼면 가장 적게 나올 것이다.
'Coding Test > 백준-Python' 카테고리의 다른 글
| 백준 / 2×n 타일링 2 / 11727번 / Python (0) | 2025.03.16 |
|---|---|
| 백준 / 괄호의 값 / 2504번 / Python (1) | 2025.03.14 |
| 백준 / A와 B / 12904번 / Python (0) | 2025.03.12 |
| 백준 / 빗물 / 14719번 / Python (0) | 2025.03.11 |
| 백준 / 토마토 / 7569번 / Python (0) | 2025.03.10 |