๋ฐฑ์ค€ - ์ฃผ์‹(Swift)

2022. 11. 8. 10:27ใ†Algorithm

๋ฐฑ์ค€ - ์ฃผ์‹(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

11501๋ฒˆ: ์ฃผ์‹

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

www.acmicpc.net

 

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

 

์ด ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ๊ฐ„๊ณผํ–ˆ๋˜ ๊ฒŒ ๋‘ ๊ฐ€์ง€ ์žˆ๋‹ค.

1. ์•ž์—์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์ฃผ์‹์˜ ๊ฐ’์ด ๋–จ์–ด์ง€๋Š” ์‹œ์ ์—์„œ ๊ตฌ์ž…ํ•œ ๊ฑธ ๋‹ค ํŒŒ๋Š” ๊ฒƒ์„ ๊ทธ๋ฆฌ๋””๋กœ ์ฑ„ํƒํ•œ ์ .

2. ์ž…๋ ฅ์ด 100,000์ด์ƒ์ธ๋ฐ readLine์„ ์‚ฌ์šฉํ•˜๋ ค ํ–ˆ๋‹ค๋Š” ์ .

 

์ด ๋‘ ๊ฐ€์ง€๊ฐ€ ํ’€์ด๋ฅผ ์กฐ๊ธˆ ๋Š๋ฆฌ๊ฒŒํ–ˆ๋‹ค.

๋ณดํ†ต ์ด๋Ÿฐ ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ๋Š” ์•ž ์ธ๋ฑ์Šค์™€ ๋’ค ์ธ๋ฑ์Šค๋งŒ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ max๊ฐ’์„ ์„ค์ •ํ•ด๋‘๊ณ  max์—๋”ฐ๋ผ์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์€ ๊ฒƒ ๊ฐ™๋‹ค. ์ด ๋ฌธ์ œ๋„ ๊ทธ๋ ‡๋‹ค. 

์˜ˆ๋ฅผ ๋“ค์–ด

1 1 3 1 4 2๋ผ๋Š” ์ž…๋ ฅ๊ฐ’์ด ์ฃผ์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.

์ฒ˜์Œ ๋‚˜์˜ ๋…ผ๋ฆฌ๋ผ๋ฉด 3์—์„œ 1์ด๋˜๋Š” ์‹œ์ ์—์„œ ์•ž์— ์‚ฐ ์ฃผ์‹๋“ค์„ ํŒ”์•„์•ผํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ตœ์ ์˜ ์ด๋“์€ 4์— ํŒŒ๋Š” ๊ฒŒ ๋งž๋‹ค. 

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์•ž์—์žˆ๋Š” ๋‚ ๋“ค์˜  ์ฃผ์‹์ด ๊ฐ’์ด ๋’ค์—์„œ ๊ฐ€์žฅ ์ตœ๊ณ ์˜ ๊ฐ’์— ํŒ”๋ ค์•ผํ•œ๋‹ค๋Š” ์•„์ด๋””์–ด๊ฐ€ ๋– ์˜ค๋ฅธ๋‹ค.

 

์ด๋Š” ๋’ค์—์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งจ๋’ค์˜ ๊ฐ’์€ max์˜ ๊ฐ’์ด๋‹ค. (๋’ค์— ์•„๋ฌด๊ฒƒ๋„ ์—†๊ธฐ ๋•Œ๋ฌธ์—) ๊ทธ๋ฆฌ๊ณ  ๋” ํฐ ๊ฐ’์ด๋‚˜์˜ค๋ฉด max๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๊ณ  ๋” ์ž‘์€ ๊ฐ’์ด ๋‚˜์˜จ๋‹ค๋ฉด max๊ฐ’์œผ๋กœ ํŒ๋งคํ•œ ๊ฐ’์„ ๋ˆ„์ ํ•˜๋ฉด ๋œ๋‹ค. 

 

๊ทธ๋ฆฌ๊ณ  fileIO๋ฅผ ์‚ฌ์šฉํ• ๋•Œ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐฐ์—ด์„ ๋ฐ›์„๋•Œ๋Š” array๋ฅผ ์ •์˜ํ•ด๋‘” ๋’ค ์นด์šดํŠธ๋งŒํผ arr.append(fIO.readInt())๋ฅผ ํ•˜๋ฉด ๋œ๋‹ค.

 

import Foundation


final class FileIO {
    private let buffer:[UInt8]
    private var index: Int = 0

    init(fileHandle: FileHandle = FileHandle.standardInput) {
        
        buffer = Array(try! fileHandle.readToEnd()!)+[UInt8(0)] // ์ธ๋ฑ์Šค ๋ฒ”์œ„ ๋„˜์–ด๊ฐ€๋Š” ๊ฒƒ ๋ฐฉ์ง€
    }

    @inline(__always) private func read() -> UInt8 {
        defer { index += 1 }

        return buffer[index]
    }

    @inline(__always) func readInt() -> Int {
        var sum = 0
        var now = read()
        var isPositive = true

        while now == 10
                || now == 32 { now = read() } // ๊ณต๋ฐฑ๊ณผ ์ค„๋ฐ”๊ฟˆ ๋ฌด์‹œ
        if now == 45 { isPositive.toggle(); now = read() } // ์Œ์ˆ˜ ์ฒ˜๋ฆฌ
        while now >= 48, now <= 57 {
            sum = sum * 10 + Int(now-48)
            now = read()
        }

        return sum * (isPositive ? 1:-1)
    }

    @inline(__always) func readString() -> String {
        var now = read()

        while now == 10 || now == 32 { now = read() } // ๊ณต๋ฐฑ๊ณผ ์ค„๋ฐ”๊ฟˆ ๋ฌด์‹œ
        let beginIndex = index-1

        while now != 10,
              now != 32,
              now != 0 { now = read() }

        return String(bytes: Array(buffer[beginIndex..<(index-1)]), encoding: .ascii)!
    }

    @inline(__always) func readByteSequenceWithoutSpaceAndLineFeed() -> [UInt8] {
        var now = read()

        while now == 10 || now == 32 { now = read() } // ๊ณต๋ฐฑ๊ณผ ์ค„๋ฐ”๊ฟˆ ๋ฌด์‹œ
        let beginIndex = index-1

        while now != 10,
              now != 32,
              now != 0 { now = read() }

        return Array(buffer[beginIndex..<(index-1)])
    }
}

import Foundation

let fIO = FileIO()

let tc = fIO.readInt()

for _ in 0..<tc {
    let n = fIO.readInt()
    var sum = 0
    var arr = [Int]()
    for _ in 0..<n {
        arr.append(fIO.readInt())
    }
    arr.reverse()
    var max = 0
    for x in arr {
        if x > max {
            max = x
        } else {
            sum += max-x
        }
    }
    print(sum)
}