CS/Algorithm
[BOJ/Python] 백준 9017번 - 크로스 컨트리
sohy
2024. 6. 5. 23:47
백준 #9017 크로스 컨트리
https://www.acmicpc.net/problem/9017
import sys
from collections import Counter
input = sys.stdin.readline
T = int(input())
for _ in range(T):
N = int(input())
teams = list(map(int, input().split()))
counter = Counter(teams)
scores = {}
rank = 1
for i in range(N):
if counter[teams[i]] == 6:
if teams[i] in scores:
scores[teams[i]].append(rank)
else:
scores[teams[i]] = [rank]
rank += 1
print(sorted(scores, key = lambda x:(sum(scores[x][0:4]), scores[x][4]))[0])
- Counter
- 배열을 인자로 넘기면 각 원소가 몇 번씩 나오는지 객체(딕셔너리 형태) 반환
파이썬 collections 모듈의 Counter 사용하기
데이터의 개수를 셀 때 매우 유용한 파이썬의 collections 모듈의 Counter 클래스Hash와 같이 알고리즘 문제를 풀 때에도 유용하게 사용할 수 있다.collections 모듈의 Counter 클래스는 별도 패키지 설치 없
velog.io