문제: 13414번: 수강신청
basic-algo-lecture/workbook/0x15.md at master · encrypted-def/basic-algo-lecture
basic-algo-lecture/workbook/0x15.md at master · encrypted-def/basic-algo-lecture
바킹독의 실전 알고리즘 강의 자료. Contribute to encrypted-def/basic-algo-lecture development by creating an account on GitHub.
github.com
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <limits>
#include <unordered_set>
#include <unordered_map>
#include <queue>
using namespace std;
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
int k, l; // 수강 가능 인원, 대기 목록의 길이
cin >> k >> l;
// key: 학번, value: 우선순위
unordered_map<string, int> persons;
int order = 0;
for (int i = 0; i < l; i++) {
string name;
cin >> name;
// 만약 이미 존재하는 학번이면 맨 뒤로 옮겨
if (persons.find(name) != persons.end()) {
persons[name] = order++;
}
else {
persons[name] = order++;
}
}
int cnt = 0;
priority_queue<pair<int, string>, vector<pair<int, string>>, greater<pair<int, string>>> orders;
for (pair<string, int> ele : persons) {
orders.push({ ele.second, ele.first });
}
while (!orders.empty() && cnt < k) {
pair<int, string> ele = orders.top();
orders.pop();
cout << ele.second << "\n";
cnt++;
}
return 0;
}
아 진짜 열받는 문제. 수강 신청 가능한 인원보다 수강 신청을 덜 하는 경우도 있어서 마지막 while 문에 !orders.empty() 구문으로 체크를 해줘야 한다.