From 2f4757f56075cc020c4295210cac0646d5b1fdbc Mon Sep 17 00:00:00 2001 From: nkey Date: Wed, 28 Jan 2026 15:53:58 +0900 Subject: [PATCH] =?UTF-8?q?9017-s3=20=EC=84=B1=EA=B3=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workbook_8708/9017-s3.py | 79 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 workbook_8708/9017-s3.py diff --git a/workbook_8708/9017-s3.py b/workbook_8708/9017-s3.py new file mode 100644 index 0000000..feefc17 --- /dev/null +++ b/workbook_8708/9017-s3.py @@ -0,0 +1,79 @@ +# 크로스 컨트리 + +import sys + +from collections import Counter + +input = sys.stdin.readline + +def exclude_team(teams): + excluded = set() + + for team in teams: + if teams[team] != 6: + excluded.add(team) + + return excluded + +def find_winning_team(rank, excluded, team_count, n): + team_score = {i: {"count": 0, "score": 0} for i in range(1, team_count+1) if i not in excluded} + first_5th = {} + now_rank = 0 + for r in rank: + if r in excluded: + continue + + now_rank += 1 + + if team_score[r]["count"] >= 4: + if first_5th.get(r, 0) == 0: + first_5th[r] = now_rank + continue + team_score[r]["score"] += (now_rank) + team_score[r]["count"] += 1 + + winner = {"team": 0, "score": float("inf"), "5th": n+1} + for team in team_score: + if team_score[team]["score"] < winner["score"]: + winner["team"], winner["score"], winner["5th"] = team, team_score[team]["score"], first_5th.get(team, n+1) + elif team_score[team]["score"] == winner["score"] and first_5th.get(team, n+1) < winner["5th"]: + winner["team"], winner["score"], winner["5th"] = team, team_score[team]["score"], first_5th.get(team, n+1) + else: + continue + + return winner["team"] + + +def game(): + n = int(input().rstrip()) + rank = list(map(int, input().rstrip().split())) + teams = Counter(rank) + excluded = exclude_team(teams) + + result = find_winning_team(rank, excluded, len(teams), n) + + return result + +def solution(): + t = int(input().rstrip()) + for _ in range(t): + result = game() + print(result) + return + + +solution() + +""" +걸린 시간: 50분 정도(score를 반대로 생각하거나, dictionary keyerror 때문에..) -> dict는 앵간하면 .get()으로 하자. + +시간 복잡도: exclude_team()은 Counter 객체 전체를 순회하기 때문에 O(m)이다. +find_winning_team()은 rank 전체를 순회하고 팀 리스트를 순회하는 것인데 set과 dict를 쓰므로 O(1)이라서 전체는 m < n이므로 O(n)이다. +game()은 Counter이므로 O(m)에다가 exclude_team()과 find_winning_team()을 돌리기 때문에 전체는 O(n)이다. +마지막으로 전체 프로그램은 game()을 t번 반복하기 때문에 전체 시간복잡도는 O(t*n)이다. +함수별로 나눠놓으니까 전체 시간복잡도를 계산하는 것이 확실히 편하다. + +해설: 먼저 6명이 아닌 팀을 counter로 알아낸 뒤 제외한다. 그 후 rank 리스트를 순차적으로 확인하면서 팀의 총점을 기록한다. +이때, 4등까지만 계산을 하도록 조건을 걸어주고, 팀의 5등이 전체 몇 등인지만 dict에 저장한다. +마지막에 총점을 비교하고, 같다면 5등까지 확인하는 절차를 거쳐서 최종 우승 팀을 결정한다. +""" \ No newline at end of file