Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 1654๋ฒˆ - ๋žœ์„ ์ž๋ฅด๊ธฐ (feat. ์ด๋ถ„ํƒ์ƒ‰)

๊ฐ์ž ๐Ÿฅ” 2022. 9. 4. 20:48
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ ๋งํฌ

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

 

1654๋ฒˆ: ๋žœ์„  ์ž๋ฅด๊ธฐ

์ฒซ์งธ ์ค„์—๋Š” ์˜ค์˜์‹์ด ์ด๋ฏธ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋žœ์„ ์˜ ๊ฐœ์ˆ˜ K, ๊ทธ๋ฆฌ๊ณ  ํ•„์š”ํ•œ ๋žœ์„ ์˜ ๊ฐœ์ˆ˜ N์ด ์ž…๋ ฅ๋œ๋‹ค. K๋Š” 1์ด์ƒ 10,000์ดํ•˜์˜ ์ •์ˆ˜์ด๊ณ , N์€ 1์ด์ƒ 1,000,000์ดํ•˜์˜ ์ •์ˆ˜์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•ญ์ƒ K โ‰ฆ N ์ด๋‹ค. ๊ทธ

www.acmicpc.net

 

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

๐Ÿ’ก ๋ฌด์–ธ๊ฐ€๋ฅผ ์ž๋ฅด๋Š” ๋ฌธ์ œ, ๊ทธ๋ฆฌ๊ณ  ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€์ถฉ.. '์ด๋ถ„ํƒ์ƒ‰์ธ๊ฐ€' ํ•˜๋Š” feel์ด ์™”๋‹ค.

 

 

2๋ฒˆ์—์„œ ์ •๋‹ต์ธ 200์ด ๋„์ถœ๋˜์—ˆ๋Š”๋ฐ ์™œ ๋ฉˆ์ถ”์ง€ ์•Š๋Š”๊ฐ€? ๊ทธ ์ด์œ ๋Š”, start์™€ end๊ฐ€ ๊ฐ™์•„์ง€๋Š” ์ง€์ ์„ ์ฐพ์•„์•ผ, ํ•ด๋‹น ํƒ์ƒ‰์„ ๋ชจ๋‘ ๋Œ๊ณ ๋‚˜์„œ ์ตœ๋Œ€๊ฐ’์„ ์ฐพ์€ ๊ฒƒ์ด๋ฏ€๋กœ, 2๋ฒˆ์—์„œ ์•„๋ฌด๋ฆฌ 200์ด๋ผ๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋„์ถœ๋์–ด๋„ ์—ฌ๊ธฐ์„œ ๋๋‚ผ ๋ฐฉ๋ฒ•์ด ์—†๋‹ค. 200์ด ์ตœ๋Œ€ ๋žœ์„  ๊ธธ์ด๋ผ๊ณ  ํ™•์‹ ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค!

 

์ด ๊ณผ์ •์„ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค. ์ด๋ถ„ํƒ์ƒ‰๋„ ๊ณต์‹? ๊ฐ™์ด ํ•˜๋‚˜๋งŒ ์‘์šฉ ํ•ด์„œ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด, ๊ทธ๋ฆฌ๊ณ  ๋ฌธ์ œ๊ฐ€ ์ด๋ถ„ํƒ์ƒ‰์ž„์„ ๋นจ๋ฆฌ ๊นจ์šฐ์นœ๋‹ค๋ฉด, ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

 

๐ŸŸ  ์ •๋‹ต์ฝ”๋“œ

https://github.com/deslog/Algorithm/blob/main/Algorithm/Boj/1654_%EB%9E%9C%EC%84%A0%20%EC%9E%90%EB%A5%B4%EA%B8%B0/main.swift

 

GitHub - deslog/Algorithm: โœจ ๋ฐฑ์ค€, ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Swift ๋ฌธ์ œํ’€์ด โœจ

โœจ ๋ฐฑ์ค€, ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Swift ๋ฌธ์ œํ’€์ด โœจ. Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

import Foundation

let kn = readLine()!.split(separator: " ").map { Int(String($0))! }
let k = kn[0]
let n = kn[1]
var cables = [Int]()
for _ in 0..<k {
    cables.append(Int(readLine()!)!)
}

func solution(k: Int, n: Int, cables: [Int]) -> Int {
    var start = 1
    var end = cables.max()!
    var result = 0

    while start <= end {
        let mid = (start + end) / 2
        var tempResult = 0
        for cable in cables {
            tempResult += cable/mid
        }

        // ๋งŒ์•ฝ tempResult๊ฐ€ n๋ณด๋‹ค ์ž‘์œผ๋ฉด ๊ธธ์ด ์ค„์—ฌ์ค˜์•ผํ•จ, end๋ฅผ mid๋กœ
        if tempResult < n {
            end = mid - 1
        } else { // ๋งŒ์•ฝ tempResult๊ฐ€ n๋ณด๋‹ค ํฌ๋ฉด, ๊ธธ์ด๋ฅผ ๋Š˜๋ ค์ค˜์•ผํ•จ start๋ฅผ mid๋กœ
            //๊ทธ๋ฆฌ๊ณ  ๊ฒฐ๊ณผ๋Š” n๋ณด๋‹ค ํฌ๊ฒŒ๋‚˜์™€๋„ ๋˜๋‹ˆ๊นŒ result(๋žœ์„ ์˜ ์ตœ๋Œ€๊ธธ์ด)๋ฅผ ๊ฐฑ์‹ ํ•˜๋ฉด์„œ ์ง„ํ–‰
            start = mid + 1
            result = mid
        }
    }
    return result
}

print(solution(k: k, n: n, cables: cables))
๋ฐ˜์‘ํ˜•