[백준 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을 사용하여 (순서, 사진) 저장

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

'스터디 > 알고리즘' 카테고리의 다른 글

[백준 2231] 분해합  (0) 2022.02.11
[백준 19532] 수학은 비대면강의입니다  (0) 2022.02.11
[백준 5597] 과제 안 내신 분...?  (0) 2022.01.13
[백준 5212] 지구 온난화  (0) 2022.01.13
[백준 2164] 카드2  (0) 2022.01.10
'스터디/알고리즘' 카테고리의 다른 글
  • [백준 2231] 분해합
  • [백준 19532] 수학은 비대면강의입니다
  • [백준 5597] 과제 안 내신 분...?
  • [백준 5212] 지구 온난화
덩이
덩이
찍먹 대마왕
  • 덩이
    Devlog
    덩이
  • 전체
    오늘
    어제
    • 분류 전체보기 (104)
      • 강의 (58)
        • SAP ERP (1)
        • KOSTA (32)
        • Inflearn (0)
        • etc (25)
      • 회사 (0)
        • 스터디 (3)
        • 전자정부 (0)
      • 학교 (15)
      • 스터디 (26)
        • 알고리즘 (21)
        • 프로젝트 (3)
        • 에러 (2)
        • 자격증 (0)
      • 기타 (2)
        • 자료 (1)
        • 회고록 (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • GitHub
    • Naver
  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
덩이
[백준 1713] 후보 추천하기
상단으로

티스토리툴바