๋ฐฑ์ค€ - ํŠธ๋Ÿญ ์ฃผ์ฐจ(Swift)

2022. 11. 8. 11:04ใ†Algorithm

๋ฐฑ์ค€ - ํŠธ๋Ÿญ ์ฃผ์ฐจ(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

2979๋ฒˆ: ํŠธ๋Ÿญ ์ฃผ์ฐจ

์ฒซ์งธ ์ค„์— ๋ฌธ์ œ์—์„œ ์„ค๋ช…ํ•œ ์ฃผ์ฐจ ์š”๊ธˆ A, B, C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ C ≤ B ≤ A ≤ 100) ๋‹ค์Œ ์„ธ ๊ฐœ ์ค„์—๋Š” ๋‘ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด ์ •์ˆ˜๋Š” ์ƒ๊ทผ์ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํŠธ๋Ÿญ์ด ์ฃผ์ฐจ์žฅ์— ๋„์ฐฉํ•œ ์‹œ๊ฐ„๊ณผ ์ฃผ์ฐจ์žฅ

www.acmicpc.net

 

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

 

ํ™•์‹คํžˆ ์œ ํ˜•๋ณ„๋กœ ํ’€๋‹ค๋ณด๋‹ˆ ๋‚ด๊ฐ€ ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์ด ์–ด๋Š ํŒŒํŠธ์ธ์ง€ ๋”ฑ ๋Š๊ปด์กŒ๋‹ค.

์ฒ˜์Œ์—๋Š” ํŠธ๋Ÿญ์ด 3๊ฐœ๋กœ ๊ณ ์ •๋˜์–ด์žˆ์œผ๋‹ˆ ์ถœ๋ฐœ์‹œ๊ฐ„์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ๊ฐ ํŠธ๋Ÿญ์˜ ์ถœ๋ฐœ์‹œ๊ฐ„๊ณผ ๋„์ฐฉ์‹œ๊ฐ„ ์‚ฌ์ด์˜ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ํ†ตํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค ํ–ˆ์œผ๋‚˜ ์ƒ๋‹นํžˆ ๋ณต์žกํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐพ์•„๋ณด๋‹ˆ ์žˆ์—ˆ๋˜ ์‹œ๊ฐ„๋งŒํผ ๊ฐ๊ฐ ๋ˆ„์ ํ•จ์œผ๋กœ์จ ๊ทธ ์‹œ๊ฐ„๋Œ€์— ๋ช‡๋Œ€์˜ ํŠธ๋Ÿญ์ด ์žˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

์ˆ˜ํ•™์ ์ธ ์‚ฌ๊ณ ๋กœ ํ‘ธ๋Š” ๊ฒƒ๋„ ์ค‘์š”ํ•˜์ง€๋งŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ ์ธ ์‚ฌ๊ณ ๋กœ ํ’€์–ด๋‚ด๋Š” ๊ฒƒ๋„ ์ƒ๋‹นํžˆ ์ค‘์š”ํ•œ ๊ฒƒ ๊ฐ™๋‹ค. 

 

import Foundation

let input = readLine()!.components(separatedBy:" ").map{Int($0)!}
let A = input[0], B = input[1], C = input[2]
var cnts = [Int](repeating: 0, count: 101)
var sum = 0
for _ in 0..<3 {
    let times = readLine()!.components(separatedBy:" ").map{Int($0)!}
    (times[0]..<times[1]).forEach { cnts[$0] += 1 }
}

for x in cnts {
    if x == 1 { // ํŠธ๋Ÿญ์ด 1๊ฐœ๋งŒ ์ฃผ์ฐจ๋˜์—ˆ์„๋•Œ
        sum += (x * A)
    } else if x == 2 {
        sum += (x * B)
    } else {
        sum += (x * C)
    }
}
print(sum)