๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 20006 - ๋žญํ‚น์ „ ๋Œ€๊ธฐ์—ด

by HandHand 2021. 4. 13.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 20006๋ฒˆ

ํ’€์ด ๊ณผ์ •

๋žญํ‚น ๋Œ€๊ธฐ์—ด ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
๋จผ์ € ๋ฐฉ์˜ ์ •๋ณด๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ํ•ด์‹œ ํ…Œ์ด๋ธ” ์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
์ด๋•Œ ํ•ด๋‹น ๋ฐฉ์— ๋ฐฉ์žฅ์˜ ์‹๋ณ„์ž์™€ ์ฐธ์—ฌ์ค‘์ธ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์„ ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
์—ฌ๊ธฐ์„œ assign_player ํ•จ์ˆ˜์—์„œ๋Š” ํ”Œ๋ ˆ์ด์–ด๋ฅผ ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” ๋ฐฉ์— ํ• ๋‹น ๊ฐ€๋Šฅํ•œ์ง€ ํŒ๋‹จํ•˜๊ณ 
๋งŒ์•ฝ ์ƒˆ๋กœ์šด ๋ฐฉ์ด ํ•„์š”ํ•  ๊ฒฝ์šฐ ๋ฐฉ์žฅ์œผ๋กœ ํ• ๋‹นํ•œ ๋’ค ์ƒˆ๋กœ์šด ๋ฐฉ์„ ์ƒ์„ฑํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys


def assign_player(player_id, player_level, rooms):
    for _, meta in rooms.items():
        master_level = players[meta['master']]
        if master_level - 10 <= player_level <= master_level + 10 and len(meta['participant']) < M - 1:
            meta['participant'].append(player_id)
            return True

    return False


def solution():
    rooms = {}

    for player_id, player_level in players.items():
        if not assign_player(player_id, int(player_level), rooms):
            rooms[len(rooms)] = {'master': player_id, 'participant': []}

    for room_id in rooms:
        master = rooms[room_id]['master']
        participants = rooms[room_id]['participant']

        if len(participants) == M - 1:
            print('Started!')
        else:
            print('Waiting!')

        for player in sorted(participants + [master]):
            print(f"{players[player]} {player}")


if __name__ == '__main__':
    P, M = list(map(int, sys.stdin.readline().split()))
    players = {}
    for _ in range(P):
        level, id = list(sys.stdin.readline().split())
        players[id] = int(level)

    solution()
๋ฐ˜์‘ํ˜•

๐Ÿ’ฌ ๋Œ“๊ธ€