๋ฐฑ์ค€ - ์ด์žฅ๋‹˜ ์ดˆ๋Œ€(Swift)

2022. 11. 7. 10:28ใ†Algorithm

๋ฐฑ์ค€ - ์ด์žฅ๋‹˜ ์ดˆ๋Œ€(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

9237๋ฒˆ: ์ด์žฅ๋‹˜ ์ดˆ๋Œ€

์ž…๋ ฅ์€ ๋‘ ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ์ฒซ์งธ ์ค„์—๋Š” ๋ฌ˜๋ชฉ์˜ ์ˆ˜ N (1 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ๊ฐ ๋‚˜๋ฌด๊ฐ€ ๋‹ค ์ž๋ผ๋Š”๋ฐ ๋ฉฐ์น ์ด ๊ฑธ๋ฆฌ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ธ ti๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ ti ≤ 1,000,000)

www.acmicpc.net

 

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

 

์ œ์ผ ๋Šฆ๊ฒŒ ๊ฑธ๋ฆฌ๋Š” ์”จ์•—๋ถ€ํ„ฐ ์‹ฌ์œผ๋ฉด ๊ฐ€์žฅ ๋นจ๋ฆฌ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋Š” ๋‚ ์งœ๊ฐ€ ๋œ๋‹ค.

 

import Foundation

let n = Int(readLine()!)!
var arr = readLine()!.components(separatedBy:" ").map{Int($0)!}

arr.sort(by: >)
var day = 2 // ๋ฌ˜๋ชฉ์„ ๊ตฌ์ž…ํ•œ ๊ฒŒ 1์ผ ์‹ฌ์–ด์ง„ ๋‚ ์งœ๋Š” ๊ทธ ๋‹ค์Œ 2์ผ
var cur = 0
for x in arr {
    if cur < x {
        cur = x
    } 
    cur -= 1
    day += 1
}

day += cur
print(day)