1+ """
2+ 콘센트에 남은 기기 중 담 순번 없거나 가장 나중인 것 부터 교체
3+ DP..?
4+ """
5+ import sys
6+ N , K = map (int ,sys .stdin .readline ().split ())
7+ #1. 사용순서
8+ use_case = list (map (int ,sys .stdin .readline ().split ()))
9+ timelines = [[] for _ in range (K + 1 )] # idx : 기기 번호, value : times(순번)
10+
11+ # 2. dp 리스트 만들기
12+ for name in range (len (use_case )): # 0~k 초
13+ for idx in range (1 ,K + 1 ): # 1~k번호 device
14+ if idx == use_case [name ]:
15+ timelines [idx ].append (name )
16+
17+ # 시간 별로 plug in 업데이트
18+ # plug in 한 기기 중 다음 업데이트가 늦는 기기 부터 plut out -> 교체
19+
20+ cnt = 0
21+ current_plug = []
22+ for time in range (K ):
23+ current_device = use_case [time ]
24+ #0. current_device가 현재 plug에 그대로 존재하는 경우 , continue
25+ if current_device in current_plug :
26+ timelines [current_device ].pop (0 )
27+ continue
28+
29+ #1. plug 가 남으면 -> 그냥 넣기
30+ if len (current_plug ) < N :
31+ current_plug .append (current_device )
32+ timelines [current_device ].pop (0 )
33+
34+ #2. plug 공간 없으면 -> plug out할 것 찾기-> 교체
35+ else :
36+ changed_device_idx = 0
37+ plug_out_hole = 0
38+ for hole in range (N ) :
39+ if len (timelines [current_plug [hole ]]) <= 0 : # 만약 다음 순번에서 사용 안하는 경우 , 바로 교체 장치로 선정
40+ plug_out_hole = hole
41+ break
42+ if timelines [current_plug [hole ]][0 ] > changed_device_idx :
43+ changed_device_idx = timelines [current_plug [hole ]][0 ] # next time
44+ plug_out_hole = hole
45+ #plug out
46+ cnt += 1
47+ current_plug .pop (plug_out_hole )
48+ # 교체
49+ current_plug .append (current_device )
50+ timelines [current_device ].pop (0 )
51+
52+ print (cnt )
0 commit comments