๋ฐฑ์ค€ - ์˜ฌ๋ฆผํ”ฝ(Swift)

2022. 11. 8. 17:04ใ†Algorithm

๋ฐฑ์ค€ - ์˜ฌ๋ฆผํ”ฝ(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

8979๋ฒˆ: ์˜ฌ๋ฆผํ”ฝ

์ž…๋ ฅ์˜ ์ฒซ ์ค„์€ ๊ตญ๊ฐ€์˜ ์ˆ˜ N(1 ≤ N ≤ 1,000)๊ณผ ๋“ฑ์ˆ˜๋ฅผ ์•Œ๊ณ  ์‹ถ์€ ๊ตญ๊ฐ€ K(1 ≤ K ≤ N)๊ฐ€ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ๊ตญ๊ฐ€๋Š” 1๋ถ€ํ„ฐ N ์‚ฌ์ด์˜ ์ •์ˆ˜๋กœ ํ‘œํ˜„๋œ๋‹ค. ์ดํ›„ N๊ฐœ์˜ ๊ฐ ์ค„์—๋Š” ์ฐจ๋ก€๋Œ€๋กœ ๊ฐ

www.acmicpc.net

 

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

 

์ด ๋ฌธ์ œ๊ฐ€ ๊ตฌํ˜„์œผ๋กœ ๋ถ„๋ฅ˜๋œ ๋ฌธ์ œ๊ฐ€ ์•„๋‹ˆ์˜€๋‹ค๋ฉด ๋‹จ์ˆœ ๊ตฌํ˜„ ๋ฌธ์ œ๋ผ๋Š” ๊ฒƒ์„ ์–ด๋–ป๊ฒŒ ์•Œ์•˜์„๊นŒ?

๋ฌธ์ œ๋Š” ๋‹จ์ˆœํžˆ ๊ธˆ, ์€, ๋™๋งŒ์„ ๋น„๊ตํ•ด์„œ ๋“ฑ์ˆ˜๋ฅผ ์ •ํ•œ๋‹ค. ๋ฌธ์ œ์˜ ์ž…๋ ฅ์กฐ๊ฑด๋„ ์ž‘๊ณ  ์ตœ์ ์„ ์ฐพ๋Š” ๋ฌธ์ œ๋Š” ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์ˆœํžˆ ๊ตฌํ˜„ํ•˜๋Š” ๋Šฅ๋ ฅ์„ ๋ณด๋Š” ๋ฌธ์ œ๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

 

๋ฌธ์ œ๊ฐ€ ์žฌ๋ฐŒ๋˜ ์ ์€ ์—ฌ๋Ÿฌ๊ฐœ์˜ ์†์„ฑ์„ ๋„ฃ์€ ํ”„๋กœํผํ‹ฐ์™€ ==ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๋ฉด์„œ ์‹ค์ œ๋กœ ๊ฐœ๋ฐœํ•˜๋Š” ๋Š๋‚Œ์ด ๋“ค์—ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ id๋ฅผ ๋„ฃ์Œ์œผ๋กœ์จ ์›ํ•˜๊ณ ์žํ•˜๋Š” ๋“ฑ์ˆ˜๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

 

import Foundation

struct Country {
    var id: Int
    var gold: Int
    var silver: Int
    var bronze: Int
    var prize: Int
    
    static func ==(_ lhs: Country, _ rhs: Country) -> Bool {
        return (lhs.gold == rhs.gold) && (lhs.silver == rhs.silver) && (lhs.bronze == rhs.bronze) 
    }
}

let input = readLine()!.components(separatedBy:" ").map{Int($0)!}
let N = input[0], Fnum = input[1]
var countries: [Country] = []
for i in 0..<N {
    let input = readLine()!.components(separatedBy:" ").map{Int($0)!}
    countries.append(Country(id: input[0], gold: input[1], silver: input[2], bronze: input[3], prize: 0))
}
countries.sort(by: {$0.gold != $1.gold 
                    ? $0.gold > $1.gold 
                    : ($0.silver == $1.silver 
                       ? $0.bronze > $1.bronze : $0.silver > $1.silver)})
var cur = countries[0]
var prize = 1
var cnt = 1
countries[0].prize = 1
for i in 1..<countries.count {
    cnt += 1
    if countries[i] == cur {
        countries[i].prize = prize
    } else {
        cur = countries[i]
        prize = cnt
        countries[i].prize = prize
    }
}

print(countries.filter{$0.id == Fnum}[0].prize)