๋ฐฑ์ค€ - ๊ธฐํƒ€์ค„(Swift)

2022. 10. 25. 18:46ใ†Algorithm

๋ฐฑ์ค€ - ๊ธฐํƒ€์ค„(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

1049๋ฒˆ: ๊ธฐํƒ€์ค„

์ฒซ์งธ ์ค„์— N๊ณผ M์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๊ณ , M์€ 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ๋ธŒ๋žœ๋“œ์˜ ํŒจํ‚ค์ง€ ๊ฐ€๊ฒฉ๊ณผ ๋‚ฑ๊ฐœ์˜ ๊ฐ€๊ฒฉ์ด ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ฃผ

www.acmicpc.net

 

 

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

 

์ฒ˜์Œ์— ์ด ๋ฌธ์ œ๋ฅผ ํ’€์„๋•Œ ํ•œ ๊ฐ€์ง€ ๋ธŒ๋žœ๋“œ๋งŒ ์ƒ€์„๋•Œ ์ตœ์†Œ๊ฐ’์ธ๊ฑธ๋กœ ์˜คํ•ด๋ฅผ ํ–ˆ์—ˆ๋‹ค. 

ํ•˜์ง€๋งŒ ์˜ˆ์ œ๋ฅผ ๋ณด๋ฉด์„œ ์•„๋‹Œ ๊ฒƒ์„ ์•Œ๊ฒŒ๋˜์—ˆ๊ณ  ํŒจํ‚ค์ง€์ค‘์— ๊ฐ€์žฅ ์ตœ์†Œ์˜ ๊ฐ’๊ณผ ๋‚ฑ๊ฐœ ์ค‘ ๊ฐ€์žฅ ์ตœ์†Œ์˜ ๊ฐ’์„ ๊ตฌํ•œ ๋’ค

1. ํŒจํ‚ค์ง€๋งŒ ์ „๋ถ€ ์ƒ€์„ ๋•Œ

2. ํŒจํ‚ค์ง€ + ๋‚ฑ๊ฐœ๋ฅผ ์ƒ€์„ ๋•Œ

3. ๋‚ฑ๊ฐœ๋กœ๋งŒ ์ƒ€์„๋•Œ

๋กœ ๋‚˜๋ˆ„์–ด์„œ ํ’€์—ˆ๋‹ค.

 

2๋ฒˆ์˜ ๊ฒฝ์šฐ ํŒจํ‚ค์ง€๋ฅผ ์ตœ๋Œ€ํ•œ ๋งŽ์ด์‚ฌ๋ฉด ์ข‹์€๋ฐ ๊ทธ ์ด์œ ๋Š” ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ๋Š” ๊ฒƒ์ด ๋‚ฑ๊ฐœ๋ฅผ ์‚ฌ๋Š”๊ฒƒ๋ณด๋‹ค ์ด๋“์ธ ๊ฒฝ์šฐ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

import Foundation

let input = readLine()!.components(separatedBy: " ").map{Int($0)!}
let n = input[0], m = input[1]
var minPac = Int.max
var minPer = Int.max
for _ in 0..<m {
    let tmp = readLine()!.components(separatedBy:" ").map{Int($0)!}
    minPac = min(minPac, tmp[0])
    minPer = min(minPer, tmp[1])
}
var pureNum = 1.0
var restNum = 0
var minNum = Int.max
if n > 6 {
    pureNum = Double(n) / Double(6) 
    restNum = n % 6    
    minNum = min(minNum, Int(ceil(pureNum) * Double(minPac)), Int(pureNum)*minPac+(restNum*minPer), n*minPer)
} else {
    minNum = min(minNum, Int(ceil(pureNum) * Double(minPac)), n*minPer)
}

print(minNum)

 

 ๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ํ’€์ด

 

์ข€ ๋” ๊น”๋”ํ•œ ๋กœ์ง์„ ๋‹ด์€ ํ’€์ด์ด๋‹ค.

๋จผ์ € ์ตœ์†Œ์™€ ํŒจํ‚ค์ง€์˜ ๊ฐ€๊ฒฉ๊ณผ ์ตœ์†Œ ๋‚ฑ๊ฐœ์˜ ๊ฐ€๊ฒฉ์„ ๊ตฌํ•œ ๋’ค, ์ตœ์†Œ ๋‚ฑ๊ฐœ์˜ ๊ฐ€๊ฒฉ*6์ด ์ตœ์†Œ ํŒจํ‚ค์ง€์˜ ๊ฐ€๊ฒฉ๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ์ตœ์†Œ ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•  ์ด์œ ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์ตœ์†Œ ๋‚ฑ๊ฐœ๋กœ๋งŒ ๊ณฑํ•ด์ค˜์„œ ์ถœ๋ ฅํ•œ๋‹ค.

 

๋งŒ์•ฝ ์ด ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ํŒจํ‚ค์ง€๋ฅผ ์ตœ๋Œ€ํ•œ ๋„ฃ์–ด์ค€ ๋’ค ๋‚˜๋จธ์ง€๋กœ ๋‚˜์˜ค๋Š” ์ค„์— ๋Œ€ํ•ด ๊ฐœ์ˆ˜ * ๋‚ฑ๊ฐœ์˜ ๊ฐ€๊ฒฉ๊ณผ ํŒจํ‚ค์ง€์˜ ๊ฐ€๊ฒฉ์„ ๋น„๊ตํ•ด์„œ total์— ๋„ฃ์„ ๊ฐ’์„ ๊ตฌํ•ด์ค€๋‹ค.

 

import Foundation

let input = readLine()!.components(separatedBy: " ").map{Int($0)!}
let n = input[0], m = input[1]
var minPac = Int.max
var minPer = Int.max
for _ in 0..<m {
    let tmp = readLine()!.components(separatedBy:" ").map{Int($0)!}
    minPac = min(minPac, tmp[0])
    minPer = min(minPer, tmp[1])
}
var total = 0
if minPac > minPer * 6 {
    total += minPer * n
} else {
    let pageCount = n / 6
    let pieceCount = n % 6
    total += pageCount * minPac
    if pieceCount * minPer < minPac {
        total += pieceCount * minPer
    } else {
        total += minPac
    }
}
print(total)

 

'Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

๋ฐฑ์ค€ - ๋ฌธ์„œ ๊ฒ€์ƒ‰(Swift)  (0) 2022.10.26
๋ฐฑ์ค€ - ์ˆ˜ ๋ฌถ๊ธฐ(Swift)  (0) 2022.10.25
๋ฐฑ์ค€ - ๋ณด์„๋„๋‘‘(Swift)  (0) 2022.10.25
๋ฐฑ์ค€ - ์บ ํ•‘(Swift)  (0) 2022.10.25
๋ฐฑ์ค€ A->B(Swift)  (0) 2022.10.24