난이도 : GOLD I
문제링크 : https://www.acmicpc.net/problem/1700
문제해결 아이디어
- 꼽아야할 제품이 이미 플러그에 꼽혀있으면 continue
- 플러그에 빈공간이 있으면 무조건 꼽는다
- 기존 플러그를 뽑아야 하는 상황에서
- 이후에 다시 꼽지 않는 플러그가 존재하면 그 플러그를 제거 하고 꼽는다
- else 가장 멀리 존재하는 플러그를 뽑는다. (인덱스 비교)
소스코드
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
order = list(map(int, input().split()))
plug = []
cnt = 0
for i in range(len(order)):
hasPlug = True
if order[i] in plug:
continue
if len(plug) < n:
plug.append(order[i])
continue
tmp = order[i:]
far_idx = -1
farVal = 0
for p in plug:
if p not in tmp:
farVal = p
break
if tmp.index(p) > far_idx:
far_idx = tmp.index(p)
farVal = p
plug.remove(farVal)
plug.append(order[i])
cnt += 1
print(cnt)