-
[1713] 후보 추천하기 (Python)[Python] 알고리즘/Silver 2021. 11. 19. 14:46
[문제]
https://www.acmicpc.net/problem/1713
알고리즘 분류는 구현, 시뮬레이션 입니다.
문제의 규칙에 주의해야 합니다.
(숫자가 같으면 한 후보이므로, 추가를 하지 않고 추천 수만 더해야 합니다.)
[코드]
import sys def find_key(dt, v): for key, value in dt.items(): if value == v: return key N = int(sys.stdin.readline()) rec_num = int(sys.stdin.readline()) li = map(int, sys.stdin.readline().split()) rec = list() rec_cnt = dict() for i in li: if len(rec) < N: if i not in rec: rec.append(i) rec_cnt[i] = rec_cnt.get(i, 0) + 1 else: if i in rec: rec_cnt[i] = rec_cnt.get(i, 0) + 1 else: m = find_key(rec_cnt, min(rec_cnt.values())) rec_cnt.pop(m) idx = rec.index(m) rec.pop(idx) rec.insert(idx, i) rec_cnt[i] = rec_cnt.get(i, 0) + 1 rec.sort() for i in rec: print(i, end=" ")
'[Python] 알고리즘 > Silver' 카테고리의 다른 글
[1120] 문자열 (Python) (0) 2021.11.21 [2312] 수 복원하기 (Python) (0) 2021.11.20 [1850] 최대공약수 (Python) (0) 2021.11.18 [1904] 01타일 (Python) (0) 2021.11.17 [2193] 이친수 (Python) (0) 2021.11.15