스터디/알고리즘

[백준 1713] 후보 추천하기

덩이 2022. 1. 18. 23:34
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

#define MAX 101

int n, m;
int recommend[MAX];


int main(){
	
	cin >> n >> m;

	vector<pair<int, int>> students(n);
    
	for (int i = 0; i < m; i++){
		int pick;
		cin >> pick;
		recommend[pick]++;

		bool check = true;
        
		for (int j = 0; j < n; j++){
			if (students[j].second == 0){
				students[j].second = pick;
				students[j].first = i;
				check = false;
				break;
			}

			else if (students[j].second == pick){
				check = false;
				break;
			}
		}

		if (check){
			int index = 0;
			for (int j = 1; j < n; j++){
				if (recommend[students[j].second] == recommend[students[index].second]){
					if (students[j].first < students[index].first)
						index = j;
				}
				else if (recommend[students[j].second] < recommend[students[index].second])
					index = j;
			}
			recommend[students[index].second] = 0;
			students[index].first = i;
			students[index].second = pick;
		}
	}

	vector<int> picture;
	for (int i = 0; i < n; i++)
		picture.push_back(students[i].second);

	sort(picture.begin(), picture.end());
    
	for (int i = 0; i < n; i++){
		if (picture[i] == 0)
			continue;
		cout << picture[i] << " ";
	}
	return 0;
}

vector pair을 사용하여 (순서, 사진) 저장

가장 추천 수가 적거나 추천 수가 같으면 더 오래된 사진을 빼는 것을 구현