import sys
input = sys.stdin.readline
plug_index,use_index = map(int,input().strip().split())
infor = list(map(int, input().strip().split()))
plug=[]
song=0
for index,name in enumerate(infor):
if(name in plug):
continue
elif(len(plug)<plug_index):
plug.append(name)
else:
max_check=0
plug_check=0
append_check=0
for plug_index,plug_name in enumerate(plug):
try:
plug_find=infor.index(plug_name,index+1)
except:
del plug[plug_index]
song+=1
plug.append(name)
append_check=1
break
if(max_check<plug_find):
max_check=plug_find
plug_check=plug_index
if(append_check==0):
del plug[plug_check]
song+=1
plug.append(name)
print(song)
해당문제는 골드1수준치고는 쉬운문제였다. 그리디 알고리즘을 이용하여 푸는 문제인데 알고리즘 자체는 간단하다.
일단 멀티탭에 자리가 있다면 꼽아주고 자리가 부족하다면 멀티탭에 해당 코드가 있는지 검사해준다. 만약 없다면 꼽아야하는 코드중에서 현재 꼽혀있는 코드와 비교한뒤 연관없는 코드를 뽑아준다. 만약 연관없는 코드가 없다면 가장 나중에 사용하는 코드를 뽑아준다. 코드를 뽑아줄때마다 횟수를 세고 해당 횟수를 출력하면 된다.