728x90

 

문제 기준에 따라서 열심히 계산해주면 될 것 같다.

학생의 번호가 1~100 / 추천 횟수가 1000회 이하이므로 100 x 1000을 해도 연산에 있어 어려움은 없을 것 같다.

 

답안 코드는 다음과 같다.

import sys
from collections import defaultdict
input = sys.stdin.readline
N = int(input()) #사진틀의 개수
m = int(input())
rcd = list(map(int,input().split()))
candidate = defaultdict(int)
date = defaultdict(int) 
cnt = 0 #사진틀의 현재 갯수
for i in range (0,m):
    #사진첩에 걸린 후보수가 N에 도달했는지 확인
    if len(list(candidate.keys())) < N:
        candidate[rcd[i]] += 1
        if date[rcd[i]] == 0:
            date[rcd[i]] = i + 1 #시점 등록     
    else: #사진첩에 걸린 후보수가 N인 경우            
        if candidate[rcd[i]] == 0:#날짜 초기화도 해야함
            number = list(candidate.keys())
            removing = [] #없앨 후보자
            min_vote = int(1e9)
            for num in number:
                if candidate[num] != 0:
                    min_vote = min(min_vote, candidate[num])
            for num in number:
                if candidate[num] == min_vote:
                    removing.append((num, date[num]))
            removing.sort(key = lambda x : x[1])
            del_candidate, _ = removing[0][0], removing[0][1]
            candidate[del_candidate] = 0
            candidate[rcd[i]] += 1 
            date[del_candidate] = 0
            date[rcd[i]] = i + 1
        else:
            candidate[rcd[i]] += 1
            
number = list(candidate.keys())
answer = []
for num in number:
    if candidate[num] != 0:
        answer.append(num)
answer.sort()
print(*answer)

 

+ Recent posts