๋ฐฑ์ค€ - ๋น—๋ฌผ(Swift)

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

๋ฐฑ์ค€ - ๋น—๋ฌผ(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

14719๋ฒˆ: ๋น—๋ฌผ

์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” 2์ฐจ์› ์„ธ๊ณ„์˜ ์„ธ๋กœ ๊ธธ์ด H๊ณผ 2์ฐจ์› ์„ธ๊ณ„์˜ ๊ฐ€๋กœ ๊ธธ์ด W๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ H, W ≤ 500) ๋‘ ๋ฒˆ์งธ ์ค„์—๋Š” ๋ธ”๋ก์ด ์Œ“์ธ ๋†’์ด๋ฅผ ์˜๋ฏธํ•˜๋Š” 0์ด์ƒ H์ดํ•˜์˜ ์ •์ˆ˜๊ฐ€ 2์ฐจ์› ์„ธ๊ณ„์˜ ๋งจ ์™ผ์ชฝ ์œ„์น˜

www.acmicpc.net

 

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

 

๋ฌธ์ œ๋ฅผ ๋ณด์ž๋งˆ์ž ์–ธ์ œ ๋น—๋ฌผ์ด ์ฐจ๋Š”์ง€ ๊ทœ์น™์„ ์ฐพ์œผ๋ ค๊ณ  ํ–ˆ๋‹ค. 

๊ทธ๋ž˜์„œ ์ฐพ์€ ๊ทœ์น™์€ ์ฒ˜์Œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•  ๋•Œ ๋“ค์–ด์˜ค๋Š” ๋ธ”๋ก์„ ๊ธฐ์ค€์„ ์œผ๋กœ ๋†“๊ณ  ๊ธฐ์ค€์„ ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ๋ธ”๋Ÿญ์ด ๋‚˜์˜ฌ๋•Œ๋งˆ๋‹ค ๊ทธ ์‚ฌ์ด์˜ ๊ฐ’๋“ค์„ ๊ธฐ์ค€์„ ์œผ๋กœ ๋น—๋ฌผ์„ ์ฑ„์›Œ์ค€๋‹ค๋Š” ๊ทœ์น™์„ ์ฐพ์•˜๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ์ฒ˜์Œ ๊ธฐ์ค€์„ ์„ ์ •ํ•  ๋•Œ๋งŒ flag๋ฅผ ์คŒ์œผ๋กœ์จ ๊ณ„์†ํ•ด์„œ ๊ธฐ์ค€์„ ์ด ๋ฆฌ์…‹๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. x๊ฐ€ 1๋ณด๋‹ค ํฌ๋‹ค๋Š” ์กฐ๊ฑด์„ ๋„ฃ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋์ด ๋น„์–ด์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

์‚ฌ์‹ค ๋ณ„๋กœ ์ข‹์ง€์•Š์€ ํ’€์ด์ธ ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ ๋‹ค๋ฅธ ๋ถ„๋“ค์˜ ํ’€์ด๋ฅผ ์ฐพ์•„๋ณด์•˜๋‹ค.

 

import Foundation

let input = readLine()!.components(separatedBy:" ").map{Int($0)!}
let H = input[0], W = input[1]
let arr = readLine()!.components(separatedBy:" ").map{Int($0)!}

var visited = [Bool](repeating: false, count: W) // ๋น—๋ฌผ์„ ์ฑ„์› ๋Š”์ง€
var flag = false 
var baseLine = H+1
var baseIdx = 0
var result = 0

for (i, x) in arr.enumerated() {
    if x >= 1 && !flag { 
        baseLine = x
        baseIdx = i
        flag = true
    } else if baseLine <= x {
        for j in baseIdx+1..<i {
            if !visited[j] {
                result += baseLine - arr[j]
                visited[j] = true
            }
        }
        baseLine = x
        baseIdx = i
    }
}
flag = false

for i in stride(from: W-1, to: -1, by: -1) {
    if arr[i] >= 1 && !flag {
        baseLine = arr[i]
        baseIdx = i
        flag = true
    } else if baseLine <= arr[i] {
        for j in stride(from: baseIdx-1, to: i, by: -1) {
            if !visited[j] {
                result += baseLine - arr[j]
                visited[j] = true
            }
        }
        baseLine = arr[i]
        baseIdx = i
    }
}

print(result)

 

 

 

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

 

ํŠน์ • ์œ„์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ์˜ ์ตœ๋Œ€๊ฐ’๊ณผ ์˜ค๋ฅธ์ชฝ์˜ ์ตœ๋Œ€๊ฐ’์ค‘์— ๋” ์ž‘์€ ๊ฐ’๋งŒํผ ๋น—๋ฌผ์„ ์ฑ„์›Œ์ฃผ๋Š” ๋ฐฉ์‹์ด๋‹ค.

์ด๋ฅผ ์œ„ํ•ด์„œ ๊ฐ ์ธ๋ฑ์Šค์—์„œ์˜ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•ด์คฌ๋‹ค.

 

import Foundation

let input = readLine()!.components(separatedBy:" ").map{Int($0)!}
let H = input[0], W = input[1]
let arr = readLine()!.components(separatedBy:" ").map{Int($0)!}

var leftMax = [Int](repeating: 0, count: arr.count)
var rightMax = [Int](repeating: 0, count: arr.count)
var result = 0
leftMax[0] = arr[0]
rightMax[arr.count-1] = arr.last!

for i in 1..<arr.count {
    leftMax[i] = max(leftMax[i-1], arr[i])
}

for i in (0..<arr.count-1).reversed() {
    rightMax[i] = max(rightMax[i+1], arr[i])
}

for i in 1..<arr.count-1 {
    result += min(leftMax[i], rightMax[i]) - arr[i]
}

print(result)