20006 랭킹전 대기열

YUZE·2025년 12월 9일
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int count = Integer.parseInt(st.nextToken());
        int limitCount = Integer.parseInt(st.nextToken());
        List<Room> rooms = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            st = new StringTokenizer(br.readLine());

            int level = Integer.parseInt(st.nextToken());
            String nickname = st.nextToken();
            Person person = new Person(nickname, level);

            if (rooms.isEmpty()) {
                Room room = new Room(person.level, limitCount);
                room.people.add(person);
                rooms.add(room);
                continue;
            }

            boolean flag = false;
            for (Room room : rooms) {
                if (level > room.level + 10 || level < room.level - 10) {
                    continue;
                }
                if (room.people.size() == room.limit) {
                    continue;
                }
                room.people.add(person);
                flag = true;
                break;
            }

            if (!flag) {
                Room room = new Room(person.level, limitCount);
                room.people.add(person);
                rooms.add(room);
            }
        }

        StringBuilder sb = new StringBuilder();

        for (Room r : rooms) {
            if (r.people.size() == r.limit) {
                sb.append("Started!");
                sb.append("\n");
            }
            else {
                sb.append("Waiting!");
                sb.append("\n");
            }

            r.people.sort(Comparator.comparing((p) -> p.name));
            for (Person p : r.people) {
                sb.append(p.level);
                sb.append(" ");
                sb.append(p.name);
                sb.append("\n");
            }
        }
        System.out.println(sb);
    }

    public static class Room {
        Integer level;
        Integer limit;
        ArrayList<Person> people = new ArrayList<>();

        public Room(Integer level, Integer limit) {
            this.level = level;
            this.limit = limit;
        }
    }

    public static class Person {
        String name;
        Integer level;

        public Person(String name, Integer level) {
            this.name = name;
            this.level = level;
        }
    }
}

완전탐색 + 구현 문제

시간 복잡도를 고려해야 한다는 조건이 나오지 않았기 때문에 위와 같이 코드를 작성했다.


간단한 로직 설명
1. list는 생성 된 room들을 담는 것. 무조건 생성 순서로 쌓이는 것이 보장됨
2. 방 생성
- list가 비어있을 경우 -> 생성 되어 있는 room이 없는 경우
- user에 맞게 매치되는 방이 없는 경우, room을 생성
3. 방 매칭
- 만원 방 빼고 방 매칭
- 매칭 안 될 경우 2 다시 수행

20006 랭킹전 대기열

profile
안녕하세요

0개의 댓글