백준
1. Python
n = int(input())
w = int(input())
num = list(map(int, input().split()))
photo = dict()
for i in range(w) :
if num[i] in photo :
photo[num[i]][0] += 1
else :
if len(photo) < n : #가득 차지 않으면 바로 추가
photo[num[i]] = [1, i]
else :
del_list = sorted(photo.items(), key= lambda x : (x[1][0] , x[1][1]) ) #가득 차면 오래된 후보 삭제 후 추가
del_key = del_list[0][0]
del(photo[del_key])
photo[num[i]] = [1, i]
ans_list = list(sorted(photo.keys()))
answer = str(ans_list[0])
for i in ans_list[1: ] :
answer += " " + str(i)
print(answer)
2. C++
#include <cstdio>
int n;
int pic_frame[20];
int num_cc;
int ord_cc[1000];
int cand_like[101];
int cand_where[101];
int cand_when[101];
int get_pic_frame() {
for (int i = 0 ; i < n ; i++) {
if (pic_frame[i] == 0) return i;
}
int res = 0;
int min_like = 1000;
int old_when = 1000;
for (int i = 0 ; i < n ; i++) {
int cur = pic_frame[i];
int tmp_like = cand_like[cur];
int tmp_when = cand_when[cur];
if (tmp_like < min_like) {
min_like = tmp_like;
old_when = tmp_when;
res = i;
}
else if (tmp_like == min_like && tmp_when < old_when) {
min_like = tmp_like;
old_when = tmp_when;
res = i;
}
}
return res;
}
int main() {
scanf("%d", &n);
scanf("%d", &num_cc);
for (int i = 0 ; i < num_cc ; i++) {
scanf("%d", &ord_cc[i]);
}
for (int i = 1 ; i <= 100 ; i++) {
cand_where[i] = -1;
cand_when[i] = -1;
}
for (int i = 0 ; i < num_cc ; i++) {
int cur = ord_cc[i];
if (cand_where[cur] != -1) {
cand_like[cur]++;
}
else {
int pos = get_pic_frame();
int delete_cand = pic_frame[pos];
if (delete_cand != 0) {
cand_where[delete_cand] = -1;
cand_like[delete_cand] = 0;
}
cand_where[cur] = pos;
cand_like[cur] = 1;
cand_when[cur] = i;
pic_frame[pos] = cur;
}
}
for (int i = 1 ; i <= 100 ; i++) {
if (cand_where[i] != -1) {
printf("%d ", i);
}
}
}