728x90
알고리즘 분류: 구현, 시뮬레이션
문제 링크: https://www.acmicpc.net/problem/1713
1713번: 후보 추천하기
첫째 줄에는 사진틀의 개수 N이 주어진다. (1 ≤ N ≤ 20) 둘째 줄에는 전체 학생의 총 추천 횟수가 주어지고, 셋째 줄에는 추천받은 학생을 나타내는 번호가 빈 칸을 사이에 두고 추천받은 순서대
www.acmicpc.net
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
vector<int> photo;
int visit[101] = {};
int n, num_cc;
cin >> n >> num_cc;
for(int i=0; i<num_cc; i++) {
int ord_cc;
cin >> ord_cc;
auto it = find(photo.begin(), photo.end(), ord_cc);
// 사진틀에 없는 경우
if(it == photo.end()) {
// 사진 틀에 없고 사이즈가 남아있는 경우 바로 추가
if(photo.size() < n) {
visit[ord_cc]++;
photo.push_back(ord_cc);
continue;
}
// 지울 학생을 찾아야함
int mn = 2e9;
for(int j=0; j<photo.size(); j++) {
mn = min(mn, visit[photo[j]]); // 가장 작은 추천수 찾기
}
int target = 0;
for(int j=0; j<photo.size(); j++) {
if(mn == visit[photo[j]]) {
target = photo[j]; // 가장 작은 추천수를 가진 오래된 학생 찾기
visit[target] = 0; // 학생 카운트 초기화
photo.erase(photo.begin() + j);
break;
}
}
photo.push_back(ord_cc); // 새로운 학생 추가
}
visit[ord_cc]++;
}
sort(photo.begin(), photo.end());
for(int it : photo) cout << it << ' ';
return 0;
}
728x90
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준 / BOJ] 1039 교환 (0) | 2021.07.09 |
---|---|
[백준 / BOJ] 1103 게임 (0) | 2021.07.09 |
[백준 / BOJ] 1062 가르침 (0) | 2021.07.09 |
[백준 / BOJ] 3055 탈출 (0) | 2021.07.08 |
[백준 / BOJ] 3425 고스택 (0) | 2021.07.08 |