๋ฐฑ์ค€ - ๋žญํ‚น์ „ ๋Œ€๊ธฐ์—ด(Swift)

2022. 11. 11. 16:36ใ†Algorithm

๋ฌธ์ œ ์ด๋ฆ„

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

20006๋ฒˆ: ๋žญํ‚น์ „ ๋Œ€๊ธฐ์—ด

๋ชจ๋“  ์ƒ์„ฑ๋œ ๋ฐฉ์— ๋Œ€ํ•ด์„œ ๊ฒŒ์ž„์˜ ์‹œ์ž‘ ์œ ๋ฌด์™€ ๋ฐฉ์— ๋“ค์–ด์žˆ๋Š” ํ”Œ๋ ˆ์ด์–ด๋“ค์˜ ๋ ˆ๋ฒจ๊ณผ ์•„์ด๋””๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์‹œ์ž‘ ์œ ๋ฌด์™€ ํ”Œ๋ ˆ์ด์–ด์˜ ์ •๋ณด๋“ค์€ ์ค„ ๋ฐ”๊ฟˆ์œผ๋กœ ๊ตฌ๋ถ„๋˜๋ฉฐ ๋ ˆ๋ฒจ๊ณผ ์•„์ด๋””๋Š” ํ•œ ์ค„์—์„œ ๊ณต๋ฐฑ

www.acmicpc.net

 

 ๋‚˜์˜ ํ’€์ด

 

๋‹จ์ˆœํ•œ ๊ตฌํ˜„๋ฌธ์ œ์˜€์ง€๋งŒ ์‹œ๊ฐ„์ด ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค.

๋‚œ ๊ตฌํ˜„๋ฌธ์ œ์— ๋งŽ์ด ์•ฝํ•œ ๊ฒƒ ๊ฐ™์€ ๋Š๋‚Œ์ด ๋“ ๋‹ค.

 

์ด ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š”๋ฐ ์‹œ๊ฐ„์ด ์˜ค๋ž˜๊ฑธ๋ ธ๋˜ ์š”์ธ์€ ๋‘ ๊ฐ€์ง€์ด๋‹ค.

 

1. ๋ฌธ์ œ ์ดํ•ด -> ๋ฌธ์ œ๋ฅผ ๊ผผ๊ผผํžˆ ์ฝ๊ณ  ์ž…๋ ฅ๊ฐ’๊ณผ ์ถœ๋ ฅ๊ฐ’์„ ์˜ˆ์ƒํ•ด์•ผํ•˜๋Š”๋ฐ ์ž…๋ ฅ๊ฐ’๊ณผ ์ถœ๋ ฅ๊ฐ’๋งŒ ๋ณด๊ณ  ๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๋ ค๊ณ ํ–ˆ๋‹ค. ๋ฌธ์ œ์ž์ฒด์—์„œ ์ž…๋ ฅ๊ฐ’๊ณผ ์ถœ๋ ฅ๊ฐ’์„ ์ดํ•ดํ•˜๊ฒŒ ํ•˜๋ ค๊ณ  ๋…ธ๋ ฅํ•˜๋‹ˆ๊นŒ ๋ฌธ์ œ๋ฅผ ์ž˜ ์ฝ์ž

2. ์ž๋ฃŒ๊ตฌ์กฐ ์ด์šฉ -> ์ฒ˜์Œ์—๋Š” rooms๋ณ€์ˆ˜๋ฅผ [๋ฐฉ๋ฒˆํ˜ธ: [ํ”Œ๋ ˆ์ด์–ด]]ํ˜•์‹์œผ๋กœ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ๋ฐฉ์ด ๋“ค์–ด์˜จ ์ˆœ์„œ๋Œ€๋กœ๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๊ณ  ํ•  ๋•Œ ์–ด๋ ค์›€์ด ์žˆ์–ด Room์ด๋ผ๋Š” ๊ตฌ์กฐ์ฒด๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค. ์ฒ˜์Œ ํ•œ ๋ฐฉ์‹์€ ๋”•์…”๋„ˆ๋ฆฌ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋“ค์–ด์˜จ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ์ด ๋˜์–ด์žˆ์ง€ ์•Š๋‹ค. ๊ทธ๋ ‡๊ธฐ์— Room๊ตฌ์กฐ์ฒด๋ฅผ ๋น ๋ฅด๊ฒŒ ์ƒ์„ฑํ•ด์•ผํ•œ๋‹ค๋Š” ๊ฒŒ ํฌ์ธํŠธ์˜€๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

 

import Foundation

let input = readLine()!.components(separatedBy:" ").map{Int($0)!}
let N = input[0], M = input[1] // ํ”Œ๋ ˆ์ด์–ด์˜ ์ˆ˜, ๋ฐฉ์˜ ์ œํ•œ

struct Player {
    let level: Int
    let name: String
}

struct Room {
    let roomRange: Int // ๋ฐฉ์˜ ์ž…์žฅ๊ฐ€๋Šฅ ๋ ˆ๋ฒจ๋ฒ”์œ„
    var roomList: [Player] //๋ฐฉ์— ๋“ค์–ด๊ฐ„ ํ”Œ๋ ˆ์ด์–ด๋“ค
    let roomId: Int // ๋ฐฉ ์ƒ์„ฑ์ˆœ์œผ๋กœ
}

// ๋ฐฉ์˜ ์ƒํƒœ
var rooms = [Room]()

var idx = 0 // ๋ฐฉ์˜ ์ˆœ์„œ
for i in 0..<N {
    let input = readLine()!.components(separatedBy:" ")
    let p1 = Player(level: Int(input[0])!, name: input[1])
    // ๋ ˆ๋ฒจ๋ฒ”์œ„์— ๋งž๊ณ  ๋ฐฉ์˜ ์ˆ˜์šฉ์น˜๊ฐ€ ๊ฝ‰์ฐจ์ง€ ์•Š๊ณ  ์ƒ์„ฑ๋œ ๋‚ ์งœ๊ฐ€ ๊ฐ€์žฅ ๋น ๋ฅธ์ˆœ์œผ๋กœ
    let tempRoom = rooms.filter{ $0.roomRange-10 <= p1.level && $0.roomRange+10 >= p1.level }
                        .filter{ $0.roomList.count < M }
                        .sorted(by: { $0.roomId < $1.roomId })
                           
    if tempRoom.isEmpty {
        idx += 1
        rooms.append(Room(roomRange: p1.level, roomList: [p1], roomId: idx))
    } else { // ๋ฐฉ์ด ๋น„์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด
        let foundIdx = rooms.firstIndex(where: {$0.roomId == tempRoom[0].roomId})!
        rooms[foundIdx].roomList.append(p1)
    }
}

for x in rooms {
    if x.roomList.count == M {
        print("Started!")
    } else {
        print("Waiting!")
    }
    let temp = x.roomList.sorted(by: {$0.name < $1.name })
    temp.forEach { print("\($0.level) \($0.name)")}
}