๋ฐฑ์ค€ - ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ ์œ„์˜ ๋กœ๋ด‡(Swift)

2022. 11. 14. 15:40ใ†Algorithm

๋ฐฑ์ค€ - ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ ์œ„์˜ ๋กœ๋ด‡(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

20055๋ฒˆ: ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ ์œ„์˜ ๋กœ๋ด‡

๊ธธ์ด๊ฐ€ N์ธ ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ๊ฐ€ ์žˆ๊ณ , ๊ธธ์ด๊ฐ€ 2N์ธ ๋ฒจํŠธ๊ฐ€ ์ด ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ๋ฅผ ์œ„์•„๋ž˜๋กœ ๊ฐ์‹ธ๋ฉฐ ๋Œ๊ณ  ์žˆ๋‹ค. ๋ฒจํŠธ๋Š” ๊ธธ์ด 1 ๊ฐ„๊ฒฉ์œผ๋กœ 2N๊ฐœ์˜ ์นธ์œผ๋กœ ๋‚˜๋‰˜์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ฐ ์นธ์—๋Š” ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด 1๋ถ€

www.acmicpc.net

 

 

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

 

๋ฌธ์ œ๋ฅผ ๊ผผ๊ผผํ•˜๊ฒŒ ์ฝ์ง€ ๋ชปํ•ด์„œ ์‹œ๊ฐ„์ด ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค.

 

๋‹ค๋ฅธ ๋ธ”๋กœ๊ทธ๋ฅผ ๋ณด๋‹ˆ ๋‚˜์ฒ˜๋Ÿผ ๋ฌธ์ œ ์ดํ•ด์— ์‹œ๊ฐ„์„ ๋งŽ์ด ์“ด ์‚ฌ๋žŒ๋“ค์ด ๋งŽ์€ ๊ฒƒ ๊ฐ™๋‹ค. 

์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ๊ฐ€ ํšŒ์ „ํ•˜๋ฉด์„œ N์—์„œ ๋กœ๋ด‡์„ ๋‚ด๋ฆฌ๋Š” ๊ฑด๋ฐ, ์•„๋ž˜์ชฝ์—์„œ๋„ ๋กœ๋ด‡์ด ์žˆ๋Š” ์ค„ ์•Œ๊ณ  ์ด๋ฅผ ๊ณ ๋ คํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์—ˆ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  ๋กœ๋ด‡์ด ์ปจ๋ฒ ์ด์–ด๋ฒจํŠธ์™€ ํ•จ๊ป˜ ํšŒ์ „์„ ํ•  ๋•Œ๋Š” ๋‚ด๊ตฌ๋„๊ฐ€ ๊ฐ์†Œํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋ฌธ์ œ์—์„œ ๋กœ๋ด‡์ด "์ด๋™"ํ• ๋•Œ๋งŒ ๋‚ด๊ตฌ๋„๊ฐ€ ๊ฐ์†Œํ•œ๋‹ค๊ณ  ์ ํ˜€์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

๋งˆ์ง€๋ง‰์œผ๋กœ ๋‹จ๊ณ„๋Š” 4๊ฐ€์ง€ ๊ณผ์ • ์ค‘ ์ฒซ๋ฒˆ์งธ ๊ณผ์ •์„ ์‹œ์ž‘ํ•จ๊ณผ ๋™์‹œ์— ๊ฒฐ์ •๋˜๊ธฐ ๋•Œ๋ฌธ์— result๋ณ€์ˆ˜๋ฅผ ์ฒ˜์Œ์— ์ฆ๊ฐ€์‹œํ‚ค๊ฒŒ ํ–ˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ์ ์šฉ์‹œ์ผฐ๋Š”๋ฐ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์™€์„œ ๋‹ค์‹œํ•œ๋ฒˆ ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•ด๋ณด๋‹ˆ ์ฒซ ๋ฒˆ์งธ ๊ณผ์ •์ธ ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ๋ฅผ ํšŒ์ „์‹œ์ผฐ์„๋•Œ N์— ํ•ด๋‹นํ•˜๋Š” ์นธ์˜ ๋กœ๋ด‡์„ ๋ฐ”๋กœ ๋‚ด๋ ค์ค˜์•ผ ํ–ˆ๋Š”๋ฐ ๋‚ด๋ ค์ฃผ์ง€ ์•Š์€ ๊ฒƒ์ด ๋ฌธ์ œ๊ฐ€ ๋˜์—ˆ๋‹ค.

 

 

 import Foundation

struct Belt {
    var robot: Bool // ๋กœ๋ด‡์ด ์žˆ๋Š”์ง€
    var duration: Int // ๋‚ด๊ตฌ๋„
}

let input = readLine()!.components(separatedBy:" ").map{Int($0)!}
let N = input[0], K = input[1] // ๋ฒจํŠธ ์นธ ์ˆ˜, ๋‚ด๊ตฌ๋„ 0์˜ ์ˆ˜์šฉ์น˜
let arr = readLine()!.components(separatedBy: " ").map{Int($0)!}
var belts = [Belt]()
for (i, x) in arr.enumerated() {
    belts.append(Belt(robot: false, duration: x))
}
var result = 0

while true {
    result += 1
    // 1
    belts.insert(belts.removeLast(), at: 0) // ๋งจ ๋งˆ์ง€๋ง‰ ๋ฒจํŠธ๋ฅผ ์•ž์œผ๋กœ
    if belts[N-1].robot == true { belts[N-1].robot = false }
    // 2
    for i in stride(from: N-2, to: -1, by: -1) { // ๋กœ๋ด‡์˜ ์ด๋™
        if belts[i].robot == true, belts[i+1].robot == false, belts[i+1].duration > 0 {
            belts[i].robot = false
            belts[i+1].robot = true
            belts[i+1].duration -= 1
            if i+1 == N-1 { belts[i+1].robot = false } // N์ธ๋ฑ์Šค์— ๋กœ๋ด‡์ด ์œ„์น˜ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด๋ ค์ค€๋‹ค.
        }
    }
    // 3
    if belts[0].robot == false && belts[0].duration > 0 {
        belts[0].duration -= 1
        belts[0].robot = true
    }
    // 4
    let zeroCount = belts.filter{$0.duration == 0}.count
    if zeroCount >= K {
        break
    }
}
print(result)

 

 

 ํ”ผ๋“œ๋ฐฑ

 

๊ตฌํ˜„๋ฌธ์ œ๋Š” ํ•ญ์ƒ ๊ทธ๋ ‡๋“ฏ ๋ฌธ์ œ๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์ดํ•ดํ•˜๊ณ  ๊ผผ๊ผผํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. 

์ด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ์ „์— ๋‚ด๊ตฌ๋„๋ฅผ ์œ„ํ•œ ๋ฐฐ์—ด๊ณผ ๊ฐ ๋ฒจํŠธ๋งˆ๋‹ค id๋ฅผ ์ฃผ๋ ค๊ณ ํ–ˆ๋Š”๋ฐ ์ด์™€ ๊ฐ™์€ ๋ถ€๋ถ„๋“ค์€ ํ•„์š”๊ฐ€ ์—†์—ˆ๋‹ค. 

 

์•„์ง ๋งŽ์ด ๋ถ€์กฑํ•œ ๊ฒƒ ๊ฐ™๋‹ค.