Patrick's Devlog

[BOJ/C++] 랭킹전 대기열(20006번) 본문

Study/Algorithms Practice

[BOJ/C++] 랭킹전 대기열(20006번)

Patrick_ 2022. 12. 2. 15:39

1. 개요

https://www.acmicpc.net/problem/20006

 

20006번: 랭킹전 대기열

모든 생성된 방에 대해서 게임의 시작 유무와 방에 들어있는 플레이어들의 레벨과 아이디를 출력한다. 시작 유무와 플레이어의 정보들은 줄 바꿈으로 구분되며 레벨과 아이디는 한 줄에서 공백

www.acmicpc.net

1-1. 설명

게임에 랭킹전 기능을 추가하려 한다. 플레이어간 실력차이가 있을 수 있으므로, 입장을 신청하면 자신과 비슷한 레벨의 플레이어들을 매칭해 게임을 시작하게 하려고한다. 시스템 조건에 맞게 최종적으로 만들어진 방의 상태와 플레이어들을 출력하는 프로그램을 작성한다. 자세한 사항은 위의 링크를 참조한다. 

1-2. 제한 사항

 - 첫 줄에 플레이어 수 p와방의 정원 m이 주어지며 p와 m은 1 이상 300 이하

 - 둘째 줄 부터 p개의 줄에 플레이어 레벨 l과 닉네임 n이 주어지며, l은 1 이상 500 이하

 - 입력된 순서대로 게임 시작하며, 닉네임은 중복되지 않고 길이는 16을 넘지 않음


2. 구현

2-1. 풀이

단순히 문제의 조건에 따라 구현하였다. 방은 map을 통해 생성해주었다. 문제를 꼼꼼히 읽지 않아 살짝 헤맸었다. 

2-2. 코드

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

struct Info {
    string name;
    int level;
};

bool compare(const Info& a, const Info& b) {
    return a.name < b.name;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    map<int, vector<Info>> rooms;
    
    int p, m;
    cin >> p >> m;

    Info player;
    int index = 0;
    bool flag = false;
    for (int i = 0; i < p; i++) {
        cin >> player.level >> player.name;
        flag = false;
        if (rooms.size() == 0) { // 아무것도 없을 때
            rooms[index++].push_back(player);
            continue;
        }
        
        for (auto it = rooms.begin(); it != rooms.end(); it++) {
            if (it->second.size() >= m) continue;
            if (it->second[0].level - 10 <= player.level && it->second[0].level + 10 >= player.level) {
                it->second.push_back(player);
                flag = true;
                break;
            }
        }

        if (flag) continue;
        rooms[index++].push_back(player);
    }

    
    for (auto it = rooms.begin(); it != rooms.end(); it++) {
        if (it->second.size() >= m) cout << "Started!\n";
        else cout << "Waiting!\n";
        sort(it->second.begin(), it->second.end(), compare);
        
        for (int i = 0; i < it->second.size(); i++) {
            cout << it->second[i].level << ' ' << it->second[i].name << "\n";
        }
    }

    return 0;
}

 

'Study > Algorithms Practice' 카테고리의 다른 글

[BOJ/C++] IF문 좀 대신 써줘(19637번)  (1) 2022.12.06
[BOJ/C++] 타노스(20310번)  (0) 2022.12.05
[BOJ/C++] 숫자고르기(2668번)  (0) 2022.12.01
[BOJ/C++] 최소 힙(1927번)  (0) 2022.11.30
[BOJ/C++] 빗물(14719번)  (0) 2022.11.29