๋ฐฑ์ค€ - ์ฃผ์œ ์†Œ(Swift)

2022. 10. 24. 09:18ใ†Algorithm

๋ฐฑ์ค€ - ์ฃผ์œ ์†Œ(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

13305๋ฒˆ: ์ฃผ์œ ์†Œ

ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ ๋‹ค์Œ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ๋„์‹œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ N(2 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” ์ธ์ ‘ํ•œ ๋‘ ๋„์‹œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋„๋กœ์˜ ๊ธธ์ด๊ฐ€ ์ œ์ผ ์™ผ์ชฝ ๋„๋กœ๋ถ€ํ„ฐ N-1

www.acmicpc.net

 

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

 

์ฒ˜์Œ ํ’€์ด๋Š” for๋ฌธ์„ ๋Œ๋•Œ๋งˆ๋‹ค ์ฃผ์œ ์†Œ์˜ ๋‚˜๋จธ์ง€ ๊ฐ’๋“ค ์ค‘์—์„œ min์„ ๊ตฌํ•˜๊ณ  ๋‚จ์€ ๋„๋กœ์˜ ๊ธธ์ด๋ฅผ ๊ณฑํ–ˆ๋Š”๋ฐ ๋‹ต์€ ๋‚˜์˜ค์ง€๋งŒ ๋ฌธ์ œ ์กฐ๊ฑด์˜ N์ด 1์–ต์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์•ผํ–ˆ๋‹ค.

 

๊ฒฐ๊ตญ 1์–ต์ด๋ผ๋Š” ์ˆซ์ž๋Š” Nํ˜น์€ logN์œผ๋กœ ๋๋‚ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๋ฒˆ๋งŒ ํƒ์ƒ‰ํ•˜๋ฉด์„œ min๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์„ ํƒํ–ˆ๋‹ค.

 

import Foundation

let a = Int(readLine()!)!

let loads = readLine()!.components(separatedBy:" ").map{Int($0)!}
let prices = readLine()!.components(separatedBy:" ").map{Int($0)!}
var min = prices[0]
var sum = 0
for i in 0..<loads.count {
    if prices[i] < min {
        min = prices[i]
    }
    sum += min * loads[i]
}
print(sum)