๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail

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

 

20207๋ฒˆ: ๋‹ฌ๋ ฅ

 ์ˆ˜ํ˜„์ด๋Š” ์ผ๋…„์˜ ๋‚ ์งœ๊ฐ€ 1์ผ๋ถ€ํ„ฐ 365์ผ๋กœ ํ‘œ์‹œ๋˜์–ด์žˆ๋Š” ๋‹ฌ๋ ฅ์„ ๊ฐ€์ง€๊ณ ์žˆ๋‹ค. ์ˆ˜ํ˜„์ด๋Š” ๋„ˆ๋ฌด๋‚˜๋„ ๊ณ„ํš์ ์ธ ์‚ฌ๋žŒ์ด๋ผ ์˜ฌ ํ•ด ์ผ์ •์„ ๋ชจ๋‘ ๊ณ„ํšํ•ด์„œ ๋‹ฌ๋ ฅ์— ํ‘œ์‹œํ•ด๋†จ๋‹ค.  ์—ฌ๋ฆ„์ด ๊ฑฐ์˜ ๋๋‚˜๊ฐ€์ž ์žฅ

www.acmicpc.net

๊ณจ๋“œ5

 

๋ฌธ์ œ

 

 

๋ฌธ์ œ ํ’€์ด

์—ฐ์†๋œ ๊ตฌ๊ฐ„์˜ ๊ฐ€๋กœ์™€ ์„ธ๋กœ๋ฅผ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค.
๊ทธ๋ฆผ์„ ํ†ตํ•ด ๋ณด๋Š” ๊ฒŒ ๋น ๋ฅผ ๊ฒƒ ๊ฐ™์•„ ๊ทธ๋ ค๋ณด์•˜๋‹ค.

 

๋จผ์ € ๊ฐ ์ผ์ž์— ํ•ด๋‹นํ•˜๋Š” ์Šค์ผ€์ค„์˜ ๊ฐœ์ˆ˜๋ฅผ days ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.

width๋Š” 0์ด ๋‚˜์˜ค์ง€ ์•Š๋Š” ์—ฐ์†๋œ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ฉด ๋˜๊ณ  height๋Š” ํ•ด๋‹น ๊ตฌ๊ฐ„์˜ ๊ฐ€์žฅ max๊ฐ’์ด ๋†’์ด๊ฐ€ ๋œ๋‹ค.

 

 

์ฝ”๋“œ

import Foundation

let n = Int(readLine()!)!
var schedules: [(start: Int, end: Int)] = []
for _ in 0..<n {
    let input = readLine()!.split(separator: " ").map {Int(String($0))!}
    let start = input[0]; let end = input[1]
    schedules.append((start, end))
}

var days: [Int] = Array(repeating: 0, count: 367) // 1์ผ ~ 365์ผ์— ๋Œ€ํ•ด ๊ฐ ์Šค์ผ€์ค„์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ–๋Š”๋‹ค.

for schedule in schedules {
    // ex) 2~4์ผ ์Šค์ผ€์ค„์ด๋ฉด 2,3,4 ์ธ๋ฑ์Šค์— +1
    for i in stride(from: schedule.start, through: schedule.end, by: 1) {
        days[i] += 1
    }
}

var width = 0
var height = 0
var answer = 0

for i in 1..<days.count {
    let day = days[i]
    // 0์ด ์•„๋‹Œ ์—ฐ์†๋œ ๊ตฌ๊ฐ„์˜ width๋ฅผ ๊ตฌํ•œ๋‹ค.
    if day > 0 {
        width += 1
        height = max(height, day) // ๋†’์ด๋Š” ํ•ด๋‹น ๊ตฌ๊ฐ„์˜ ์ตœ๋Œ€ ์Šค์ผ€์ค„ ๊ฐœ์ˆ˜
    }
    
    // ์—ฐ์†๋œ ๊ตฌ๊ฐ„์ด ๋๋‚ฌ์œผ๋ฉด
    else if day == 0 {
        answer += width*height
        width = 0
        height = 0
    }
}

print(answer)

 

 

ํ›„๊ธฐ

์ •๋ ฌํ•˜๊ณ  ๋ธ”๋Ÿญ๋“ค์„ ์ €์žฅํ•˜๊ณ  ๋ถ™์ด๊ณ ... ๋งค์šฐ ๋ณต์žกํ•œ ๋ฐฉ๋ฒ•๋งŒ ์ƒ๊ฐํ•ด๋‚ด๋‹ค ๊ฐ€๋กœ๋Š” ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ๋†’์ด๋Š” ๊ตฌํ•˜๊ธฐ๊ฐ€ ๊นŒ๋‹ค๋กœ์›Œ ๋ณด์˜€๋‹ค. 

but ์ƒ๊ฐ๋ณด๋‹ค ๊ฐ„๋‹จํ•œ ์•„์ด๋””์–ด๋กœ ํ’€ ์ˆ˜ ์žˆ์–ด ์ •๋ฆฌํ•ด ๋ณด์•˜๋‹ค.

 

profile

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun

@iosun

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

profile on loading

Loading...