๋ฐฑ์ค€ - NBA๋†๊ตฌ(Swift)

2022. 11. 10. 16:44ใ†Algorithm

๋ฐฑ์ค€ - NBA๋†๊ตฌ(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

2852๋ฒˆ: NBA ๋†๊ตฌ

์ฒซ์งธ ์ค„์— ๊ณจ์ด ๋“ค์–ด๊ฐ„ ํšŸ์ˆ˜ N(1<=N<=100)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๋“์  ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋“์  ์ •๋ณด๋Š” ๋“์ ํ•œ ํŒ€์˜ ๋ฒˆํ˜ธ์™€ ๋“์ ํ•œ ์‹œ๊ฐ„์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ํŒ€ ๋ฒˆํ˜ธ๋Š” 1 ๋˜๋Š” 2์ด๋‹ค. ๋“

www.acmicpc.net

 

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

 

๋ฌธ์ œ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ดํ•ดํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค.

๊ทธ์ € ๋ฌธ์ œ์˜ ์š”๊ตฌ์‚ฌํ•ญ๋Œ€๋กœ ๋ฌผ ํ๋ฅด๋“ฏ์ด ๊ตฌํ˜„๋งŒ ํ•˜๋ฉด ๋˜๋Š”์ค„ ์•Œ์•˜๋Š”๋ฐ ๊ทœ์น™์„ ์ฐพ์•„์•ผ ํ–ˆ๋‹ค.

 

๋จผ์ € A, BํŒ€ ๋‘๊ฐœ์˜ ํŒ€๋งŒ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— 1๊ณผ 2๋กœ ํ•˜๋“œ์ฝ”๋”ฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด์•ผ ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐ์„ ํ–ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋‘˜ ์ค‘ ํ•˜๋‚˜์˜ ํŒ€์ด ์ฒ˜์Œ ์ด๊ธฐ๋Š” ์‹œ์ ๊ณผ ๋น„๊ธฐ๋Š” ์‹œ์ ์„ ๋ถ„๊ธฐ๋ณ„๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒŒ ํฌ์ธํŠธ์˜€๋‹ค.

๋จผ์ € ํ•˜๋‚˜์˜ ํŒ€์ด ์ฒ˜์Œ ์ด๊ธฐ๋Š” ์‹œ์ ์—๋Š” ๊ทธ ํŒ€์˜ ๋“์ ์‹œ๊ฐ„์„ ๋‹ด์•„์•ผ ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค๋ฅธ ํŒ€์ด ์ฒ˜์Œ ์ด๊ธด ํŒ€๊ณผ ์Šค์ฝ”์–ด๊ฐ€ ๊ฐ™์•„์ง€๊ธฐ ์ „๊นŒ์ง€๋Š” ์ง€๋‚˜์น˜๊ณ  ์Šค์ฝ”์–ด๊ฐ€ ๊ฐ™์•„์งˆ ๋•Œ ํ˜„์žฌ ํƒ์ƒ‰์ค‘์ธ ์‹œ๊ฐ„๊ณผ ์ฒ˜์Œ ๋‹ด์•˜๋˜ ์‹œ๊ฐ„๊ณผ์˜ ์ฐจ์ด๋ฅผ ๋ˆ„์ ์‹œ์ผฐ๋‹ค.

 

import Foundation

let n = Int(readLine()!)!
var dic: [Int: Int] = [1: 0, 2: 0]

var flag = false // ๊ณจ์„ ๋” ๋งŽ์ด ๋„ฃ์€ ํŒ€์ด ์ƒ๊ธธ๋•Œ ์ฒ˜์Œ์—๋งŒ ์‹œ๊ฐ„์„ ๋‹ด๊ธฐ์œ„ํ•œ ํ”Œ๋ž˜๊ทธ
var oneScore = 0 // ์ฒซ ๋ฒˆ์งธ ํŒ€์˜ ๋“์ 
var secondScore = 0 // ๋‘ ๋ฒˆ์งธ ํŒ€์˜ ๋“์ 
var curTime = "00:00" // ๊ณจ์„ ๋” ๋งŽ์ด ๋„ฃ์€ ํŒ€์ด ์ƒ๊ธธ๋•Œ ์ฒ˜์Œ์˜ ์‹œ๊ฐ„๋Œ€
for _ in 0..<n {
    let input = readLine()!.components(separatedBy:" ")
    let team = Int(input[0])!, time = input[1]
    switch team {
        case 1:
        oneScore += 1
        case 2:
        secondScore += 1
        default:
        break
    }
    // ๋‘๊ฐœ์˜ ํŒ€ ์ค‘ ํ•œ ํŒ€์ด ์Šค์ฝ”์–ด๊ฐ€ ๋” ๋งŽ์•„์งˆ๋•Œ(flag๋กœ ์ธํ•œ ์ฒ˜์Œ์—๋งŒ)
    if (oneScore > secondScore || oneScore < secondScore) && !flag {
        curTime = time 
        flag = true
    } 
    // ์Šค์ฝ”์–ด๊ฐ€ ๊ฐ™์•„์งˆ๋•Œ ์ ์ˆ˜ ๋ˆ„์ 
    if oneScore == secondScore {
        if team == 2 {
            dic[1]! += (parseStringToSecond(time)-parseStringToSecond(curTime))
        } else  {
            dic[2]! += (parseStringToSecond(time)-parseStringToSecond(curTime))
        }
        flag = false
    } 
}
// ๋๋‚˜๊ณ  ๋“์  ๋„ฃ์€ ํŒ€์ด ๋‚จ์•„์žˆ์„ ๋•Œ ๋งˆ๋ฌด๋ฆฌ ์‹œ๊ฐ„์—์„œ ๋นผ์„œ ๋ˆ„์ 
if oneScore > secondScore {
    dic[1]! += parseStringToSecond("48:00") - parseStringToSecond(curTime)
} else if oneScore < secondScore {
    dic[2]! += parseStringToSecond("48:00") - parseStringToSecond(curTime)
}

print(parseSecondToString(dic[1]!))
print(parseSecondToString(dic[2]!))

// ์‹œ๊ฐ„String์„ ์ดˆ๋‹จ์œ„๋กœ ๋ณ€๊ฒฝ
func parseStringToSecond(_ time: String) -> Int {
    let times = time.split(separator: ":").map{Int($0)!}
    return times[0]*60 + times[1]
} 

// ์ดˆ๋‹จ์œ„๋ฅผ ์‹œ๊ฐ„String์œผ๋กœ ๋ณ€๊ฒฝ
func parseSecondToString(_ time: Int) -> String {
    let m = time / 60
    let c = time % 60
    var str = ""
    if m < 10 {
        str += "0\(m)"
    } else { str += "\(m)"}
    str += ":"
    if c < 10 {
        str += "0\(c)"
    } else { str += "\(c)"}
    return str
}

 

 ํ”ผ๋“œ๋ฐฑ

 

์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋Š” ๋น ๋ฅด๊ฒŒ ๊ทœ์น™์„ ์ฐพ๋Š” ๊ฒŒ ์ค‘์š”ํ•˜๋‹ค.

์–ด๋Š ์‹œ์ ์—์„œ ๊ฐ’์ด ๋ˆ„์ ํ•˜๊ณ , ๋ฆฌ์…‹ํ•ด์•ผํ•˜๋Š”์ง€๋งŒ ์ •ํ™•ํ•˜๊ฒŒ ์บ์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ๋” ๋น ๋ฅด๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.