๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ฏ | ๋ฐฑ์ค€/๐Ÿ™‚ | Silver

[Kotlin, S5] ๋ฐฑ์ค€ 32344๋ฒˆ ์œ ๋ฌผ ๋ฐœ๊ตด

by immgga 2024. 9. 23.

์ถœ์ฒ˜: unsplash.com

 

์œ ๋ฌผ ๋ฐœ๊ตด(32344๋ฒˆ)

Silver 5

#๊ตฌํ˜„ #๊ธฐํ•˜ํ•™

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

 

๋ฌธ์ œ ๋‚ด์šฉ

 

 

๋ฌธ์ œ ์ ‘๊ทผ

์šด๋™์žฅ์— a๋ฒˆ ์œ ๋ฌผ์ด w, h ์œ„์น˜์— ๋ฌปํ˜€ ์žˆ๋‹ค.

a๋ฒˆ ์œ ๋ฌผ ์กฐ๊ฐ๋“ค์˜ ์œ„์น˜ ์ •๋ณด๋ฅผ ์ด์šฉํ•ด a๋ฒˆ ์œ ๋ฌผ์˜ ์˜ˆ์ƒ ํฌ๊ธฐ๋ฅผ ์ง์‚ฌ๊ฐํ˜•์œผ๋กœ ์ •์˜ํ•œ๋‹ค.

์ง์‚ฌ๊ฐํ˜•์˜ ๋„“์ด๊ฐ€ ๊ฐ€์žฅ ํฐ ์œ ๋ฌผ์˜ ๋ฒˆํ˜ธ์™€ ๊ทธ ๋„“์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๋งŒ์•ฝ ๋„“์ด๊ฐ€ ๋™์ผํ•˜๋‹ค๋ฉด ์œ ๋ฌผ ๋ฒˆํ˜ธ๊ฐ€ ๋” ์ž‘์€ ๊ฒŒ ๋” ์šฐ์„ ์ด๋‹ค.

 

์œ ๋ฌผ์˜ ์˜ˆ์ƒ ํฌ๊ธฐ ์ง์‚ฌ๊ฐํ˜•์„ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด 4๊ฐœ์˜ ์œ„์น˜์— ์œ ๋ฌผ์ด ์žˆ์„ ๋•Œ

์  a, b, c, d๊ฐ€ ์œ ๋ฌผ์˜ ์œ„์น˜๋ผ๊ณ  ํ•˜๋ฉด ์œ ๋ฌผ์˜ ์˜ˆ์ƒ ํฌ๊ธฐ๋Š” ์‚ฌ๊ฐํ˜• efgh์™€ ๊ฐ™๋‹ค.

์œ„ ์‚ฌ์ง„์œผ๋กœ ๊ฐ€์žฅ ํฐ x์ถ•(d)๊ณผ ๊ฐ€์žฅ ์ž‘์€ x์ถ•(a), ๊ทธ๋ฆฌ๊ณ  ๊ฐ€์žฅ ํฐ y์ถ•(c)๊ณผ ๊ฐ€์žฅ ์ž‘์€ y์ถ•(b)์˜ ๊ฐ’์„ ์ด์šฉํ•ด ์œ„ ๋„ํ˜•์˜ ๋„“์ด๋ฅผ ๊ตฌํ•   ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

 

๊ฒฐ๋ก ์€ (x๊ฐ’์˜ ์ตœ๋Œ“๊ฐ’ -  x๊ฐ’์˜ ์ตœ์†Ÿ๊ฐ’ + 1) * (y๊ฐ’์˜ ์ตœ๋Œ“๊ฐ’ - y๊ฐ’์˜ ์ตœ์†Ÿ๊ฐ’ + 1)์˜ ๊ณต์‹์œผ๋กœ ์œ ๋ฌผ์˜ ์˜ˆ์ƒ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ์ฝ”๋“œ

๋”๋ณด๊ธฐ
import java.io.BufferedReader
import java.io.InputStreamReader

fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {
    val (r, c) = readLine().split(" ").map { it.toInt() }
    val relicCnt = readLine().toInt()
    val relics = mutableMapOf<Int, MutableList<List<Int>>>()

    for (i in 0 until relicCnt) {
        val (relicsNumber, w, h) = readLine().split(" ").map { it.toInt() }

        if (!relics.containsKey(relicsNumber)) {
            relics[relicsNumber] = mutableListOf(listOf(w, h))
        } else {
            relics[relicsNumber]?.add(listOf(w, h))
        }
    }

    var answer = Pair(0, 0L)
    for (relic in relics) {
        val area = getArea(relic.value)
        if (answer.second < area) {
            answer = Pair(relic.key, area)
        } else if (answer.second == area && answer.first > relic.key) {
            answer = Pair(relic.key, area)
        }
    }

    println("${answer.first} ${answer.second}")
}

private fun getArea(relicLocation: MutableList<List<Int>>): Long {
    var wMax = 0
    var wMin = Int.MAX_VALUE

    var hMax = 0
    var hMin = Int.MAX_VALUE

    for (location in relicLocation) {
        if (location[0] > wMax) wMax = location[0]
        if (location[0] < wMin) wMin = location[0]

        if (location[1] > hMax) hMax = location[1]
        if (location[1] < hMin) hMin = location[1]
    }

    return (wMax - wMin + 1) * (hMax - hMin + 1).toLong()
}

 

๋ฌธ์ œ ํ’€์ด

์œ ๋ฌผ ๋ฒˆํ˜ธ์— ๋”ฐ๋ฅธ ์œ ๋ฌผ ์กฐ๊ฐ์˜ ์œ„์น˜๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด mutableMap์„ ์ด์šฉํ•ด ์ฃผ์—ˆ๋‹ค.

map์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , map์˜ ๋ฐ์ดํ„ฐ๋“ค์„ ์ด์šฉํ•ด ๊ฐ ์œ ๋ฌผ์˜ ์˜ˆ์ƒ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•ด์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

๋งŒ์•ฝ ๊ฐ’์ด ๊ฐ™๋‹ค๋ฉด ์œ ๋ฌผ ๋ฒˆํ˜ธ๊ฐ€ ๋” ์ž‘์€ ๊ฒƒ์œผ๋กœ ๋ฐ”๋€๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •

๊ท€์ฐฎ์€ ๊ตฌํ˜„ ๋ฌธ์ œ์ด๋‹ค.

์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’์„ ์ด์šฉํ•ด ์œ ๋ฌผ์˜ ์˜ˆ์ƒ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•˜๋Š” ์•„์ด๋””์–ด๋ฅผ ๋– ์˜ฌ๋ฆฌ๋Š” ๋ฐ ์ƒ๊ฐ์ด ์ข€ ํ•„์š”ํ•˜๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์œ ๋ฌผ์˜ ์˜ˆ์ƒ ํฌ๊ธฐ๊ฐ€ Int ๋ฒ”์œ„๋ฅผ ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ์œ ์˜ํ•ด์•ผ ํ•œ๋‹ค.

๋˜ํ•œ ์œ ๋ฌผ์˜ ์˜ˆ์ƒ ํฌ๊ธฐ๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ๋” ์ž‘์€ ๋ฒˆํ˜ธ์ธ ์œ ๋ฌผ์ด ๋” ์šฐ์„ ์œผ๋กœ ์ถœ๋ ฅ๋ผ์•ผ ํ•œ๋‹ค.

์ด๋ฅผ ๊นŒ๋จน์ง€ ๋ง์ž.

728x90