[백준] 20006 랭킹전 대기열 C++알고리즘/백준2026. 2. 6. 12:43
Table of Contents
https://www.acmicpc.net/problem/20006

해설
구현 문제이다.
조건 그대로 구현하면되는데 2번 조건에 대해서 이차원 배열을 통해서 해당 i번째 방의 정원이 다 찼는지 확인할 수 있도록 하고 입장 가능한 방이 여러개라면 먼저 생성된 방을 탐색할 수 있도록 해주었다.
이차원 배열을 for문으로 순서대로 돌면 방정원이 다 찼는지 확인할 수 있고 먼저 생성된 방부터 입장이 가능하다.
코드를 보고 이해를 하도록 하자.
코드
#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <algorithm>
#include <stack>
#include <sstream>
#include <set>
#include <unordered_set>
#include <cmath>
using namespace std;
using ll = long long;
#define endl "\n"
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int p, m, l;
string n;
cin >> p >> m;
vector<vector<pair<int, string>>> rooms;
rooms.resize(p, vector<pair<int, string>>());
int tp = p;
while (tp--)
{
cin >> l >> n;
for (int i = 0; i < p; ++i) // 방이 여러개라면 먼저 생성된 방 부터 입장하도록 유도
{
if (rooms[i].size() == 0) // i 번 방이 비었다면 여기에 넣는다.
{
rooms[i].push_back({ l, n });
break;
}
else
{
int minLevel = rooms[i].front().first - 10;
int maxLevel = rooms[i].front().first + 10;
if (l < minLevel || l > maxLevel) continue; // min, max Level을 벗어나는지 확인한다.
if (rooms[i].size() == m) continue; // 정원이 다 찼는지 확인한다.
rooms[i].push_back({ l, n }); // 모든 경우를 통과 했다면 해당 방에 집어 플레이어를 집어 넣고 종료한다.
break;
}
}
}
// 닉네임을 기준으로 오름차순 정렬한다.
for (int i = 0; i < p; ++i)
{
if (rooms[i].empty()) continue;
sort(rooms[i].begin(), rooms[i].end(), [](auto& a, auto& b)
{
return a.second < b.second;
});
}
for (int i = 0; i < p; ++i)
{
if (rooms[i].empty()) continue;
if (rooms[i].size() == m)
{
cout << "Started!" << endl;
for (auto pair : rooms[i])
{
cout << pair.first << " " << pair.second << endl;
}
}
else if (rooms[i].size() < m)
{
cout << "Waiting!" << endl;
for (auto pair : rooms[i])
{
cout << pair.first << " " << pair.second << endl;
}
}
}
return 0;
}
'알고리즘 > 백준' 카테고리의 다른 글
| [백준] 17276 배열 돌리기 C++ (0) | 2026.02.09 |
|---|---|
| [백준] 1138 한 줄로 서기 C++ (0) | 2026.02.07 |
| [백준] 1913 달팽이 C++ (0) | 2026.02.05 |
| [백준] 20546 🐜 기적의 매매법 🐜 C++ (0) | 2026.02.04 |
| [백준] 풍선 터뜨리기 C++ (0) | 2026.02.02 |
@CGNY :: 김놀자
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!