chae._.chae

[Algorithm] 백준 #9017 크로스 컨트리 본문

파이썬 알고리즘/BOJ

[Algorithm] 백준 #9017 크로스 컨트리

walbe0528 2024. 7. 7. 12:05
728x90
반응형

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

 

 

import sys
from collections import defaultdict
input = sys.stdin.readline

T = int(input())
while T:
    # 각 팀당 선수 몇명인지 계산후, 여섯보다 작으면 모두 삭제
    N = int(input())
    players = list(map(int, input().split()))
    team_count = defaultdict(int)  # 선수 카운트
    team_dict = defaultdict(list)
    for i in players:
        team_count[i] += 1
    # print(team_count)  # {1: 6, 2: 2, 3: 6, 4: 1}
    # 제거할 팀
    teams_to_remove = [key for key, value in team_count.items() if value < 6]
    players = [player for player in players if player not in teams_to_remove]
    for key in teams_to_remove:
        del team_count[key]

    # 팀별로 점수 저장
    for index, player in enumerate(players):
        team_dict[player].append(index+1)

    # 각 팀별 상위 4명 점수 계산 & 다섯번째 선수 점수 기록
    team_scores={}
    fifth_scores = {}
    for team, scores in team_dict.items():
        scores.sort()
        team_scores[team] = sum(scores[:4])
        fifth_scores[team] = scores[4]
    # print(team_scores)  # {1: 18, 3: 18}

    min_score = float('inf')  # 양의 무한대
    min_team = None
    for team, score in team_scores.items():
        if score < min_score or (score == min_score and fifth_scores[team] < fifth_scores[min_team]):
            min_score = score
            min_team = team
    print(min_team)
    # print(players)  # [1, 3, 3, 1, 3, 1, 1, 3, 1, 3, 3, 1]
    T -= 1

 

  1. 각 팀당 선수가 몇명인지 계산하고 6명보다 작으면 모두 삭제
  2. team_dict에 팀별로 점수를 저장. key:팀, value:점수리스트
  3. 팀별 상위 4명 점수 계산 & 다섯번째 선수의 점수 기록

 

📌 다시 봐야 할 부분

일단.. 천천히 구현해나가면 할수있음

 

  • 딕서너리에 value값으로 리스트 넣고싶음 defaultdict(list) 이렇게 작성 가능하다
  • enumerate( ): 리스트의 원소에 순서값을 부여해주는 함수. 인덱스와 원소에 동시 접근 방법
  • min_score = float('inf') # 양의 무한대 설정
728x90