Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14-alstjr7437 #48

Merged
merged 2 commits into from
Mar 7, 2024
Merged

14-alstjr7437 #48

merged 2 commits into from
Mar 7, 2024

Conversation

alstjr7437
Copy link
Member

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋งˆ์ธํฌ๋ž˜ํ”„ํŠธ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

50๋ถ„(30๋ถ„ ์งœ๋ณด๋‹ค๊ฐ€ ์•„์ด๋””์–ด๋ฅผ ์ธํ„ฐ๋„ท์— ์ฐธ๊ณ ํ•จ..)

โš ๏ธ ์ž˜๋ชป๋œ ์ƒ๊ฐ..

์šฐ์„  ๋ธŒ๋ฃจํŠธํฌ์Šค๊ธธ๋ž˜ ๋ธ”๋Ÿญ์„ ๋‹ค ์ถ”๊ฐ€ํ•˜๊ณ  ๊ทธ ๋ธ”๋Ÿญ์˜ ์ œ์ผ ๋งŽ์€ ์ธต์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ  ๊ทธ ์ธต์ˆ˜๋ฅผ ํ† ๋Œ€๋กœ ๊ณ„์‚ฐํ•ด์„œ ์‹œ๊ฐ„์ดˆ์™€ ์ธต์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋ ค๊ณ  ํ–ˆ์ง€๋งŒ
๊ตฌํ˜„๋ถ€๋ถ„์—์„œ ๊ณ„์† ๋ญ”๊ฐ€ ์•„๋‹Œ ๊ฒƒ ๊ฐ™์•„์„œ ๊ฒฐ๊ตญ ์ธํ„ฐ๋„ท ์ฐธ๊ณ ๋ฅผ ํ–ˆ๋‹ค..

๐Ÿ’ก ์ฐธ๊ณ ํ•œ ์•„์ด๋””์–ด

๋ธŒ๋ฃจํŠธํฌ์Šค

brute: ๋ฌด์‹ํ•œ, force: ํž˜ -> ๋ฌด์‹ํ•œ ํž˜์œผ๋กœ ํ•ด๊ฒฐ์„ ํ•˜๋Š” ๊ฒƒ์ธ๋ฐ ์ •๋ง ๋ฌด์‹ํ•œ ํž˜์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด์˜€๋‹ค..
์™„์ „ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜. ์ฆ‰, ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์š”๊ตฌ์กฐ๊ฑด์— ์ถฉ์กฑ๋˜๋Š” ๊ฒฐ๊ณผ๋งŒ์„ ๊ฐ€์ ธ์˜จ๋‹ค.
ํ•ด๊ฐ€ ์กด์žฌํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋˜๋Š” ๋ชจ๋“  ์˜์—ญ์„ ์ „์ฒด ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•

์œ„์™€ ๊ฐ™์ด ๋ธŒ๋ฃจํŠธํฌ์Šค์˜ ์™„์ „ ํƒ์ƒ‰ ๊ธฐ๋ฒ•์œผ๋กœ ๊ทธ๋ƒฅ 256์ธต๊นŒ์ง€์˜ ๋ชจ๋“  ์ธต์„ ์‚ดํŽด๋ณด๋Š” ๋ฐฉ๋ฒ•์ด์˜€๋‹ค...




image

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๊ทธ๋ž˜์„œ ๊ทธ๋ƒฅ ์œ„์™€ ๊ฐ™์€ 64๊นŒ์ง€ ์žˆ์œผ๋ฉด

  1. ๋ฌธ์ œ์— ์žˆ๋Š” 2์ฐจ์› ๋ฐฐ์—ด(ground)์„ ์šฐ์„  ๋งŒ๋“ ๋‹ค.
  2. ์ •๋‹ต์„ ์ €์žฅํ•  ์‹œ๊ฐ„์ดˆ(answer)์™€ ์ธต(idx)๋ฅผ ๋งŒ๋“ ๋‹ค.
  3. 0 ~ 256์ธต๊นŒ์ง€ ๋ฐ˜๋ณต๋ฌธ(floor)์„ ์ „๋ถ€ ๋Œ๋ฆฐ๋‹ค.
    1. ๋ธ”๋ก์„ ๋นผ๋Š” ๊ฒƒ(task1)๊ณผ ์ถ”๊ฐ€(task2)ํ•˜๋Š” ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค.
    2. 2์ฐจ์› ๋ฐฐ์—ด(ground)์˜ ๋ถ€๋ถ„๊ณผ ์ธต์ˆ˜์˜ ์ˆ˜(floor)๋ฅผ ๋น„๊ตํ•œ๋‹ค.
      1. ๋ธ”๋ก์ด ์ธต์ˆ˜๋ณด๋‹ค ํฌ๋ฉด ๋นผ๋Š” ๋ถ€๋ถ„์— ๋„ฃ๋Š”๋‹ค.(๋นผ์„œ ์ธต์— ๋งž์ถฐ์•ผํ•˜๋‹ˆ)
      2. ๋ธ”๋ก์ด ์ธต์ˆ˜๋ณด๋‹ค ์ž‘์œผ๋ฉด ์ถ”๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„์— ๋„ฃ๋Š”๋‹ค.(๋” ์ถ”๊ฐ€ํ•ด์„œ ์ธต์— ๋งž์ถฐ์•ผํ•˜๋‹ˆ)
    3. ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ > ๊ฐ€์ง€๊ณ  ์žˆ๋˜ ๋ธ”๋ก + ๋ธ”๋ก์„ ๋นผ์„œ ๊ฐ€์ ธ์˜จ ๊ฒƒ์œผ๋กœ ์ถ”๊ฐ€ํ•œ ๊ฒƒ์ด ๋” ๋งŽ์œผ๋ฉด ์•ˆ๋˜๋ฏ€๋กœ continue
    4. ์‹œ๊ฐ„์„ ๋น„๊ตํ•ด์„œ ํ˜„์žฌ ์‹œ๊ฐ„์ด ๋”์ž‘์œผ๋ฉด ์‹œ๊ฐ„(count)๊ณผ ์ธต(idx)๋ฅผ ์ €์žฅ




์œ„์™€ ๊ฐ™์ด ์ˆœ์„œ๋กœ ๋ณด๋ฉด ์ž˜ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ.. ์•„๋ž˜ ์‚ฌ์ง„์„ ๋ณด๋ฉด ์กฐ๊ธˆ ๋”์ดํ•ด๊ฐ€ ๊ฐˆ๊ฒ๋‹ˆ๋‹ค!(๊ธ€๋กœ๋Š” ์ „ ์•ˆ๋˜์„œ ๊ทธ๋ฆฌ๋ฉด์„œ ํ–ˆ๋˜๊ฑฐ์ž„ ์‚ฌ์‹ค)
image




๐Ÿ’ป ์ฝ”๋“œ

import sys
input = sys.stdin.readline

n, m, b = map(int, input().split())

# 1. 2์ฐจ์› ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ
ground = []
for i in range(n):
    ground.append(list(map(int, input().split())))

# 2. ์ธต์ˆ˜์™€ ์‹œ๊ฐ„ ์ €์žฅํ•  ๋ณ€์ˆ˜ ๋งŒ๋“ค๊ธฐ(์ตœ์†Œ์‹œ๊ฐ„์„ ํ•ด์•ผํ•˜๋‹ˆ ์ตœ๋Œ€๊ฐ’ ๋„ฃ์–ด๋†“๊ธฐ)
answer = int(1e9)
idx = 0

# 3
for floor in range(257):
    # 3-1
    # task1 -> ๋ธ”๋ก์„ ๋นผ๋Š” ๊ฒƒ
    # task2 -> ๋ธ”๋ก์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ
    task1 = 0
    task2 = 0
    # 3-2
    for x in range(n):
        for y in range(m):
            # ๋ธ”๋ก์ด ์ธต์ˆ˜๋ณด๋‹ค ํฌ๋ฉด
            if ground[x][y] > floor:
                task1 += ground[x][y] - floor 
            # ๋ธ”๋ก์ด ์ธต์ˆ˜๋ณด๋‹ค ์ž‘์œผ๋ฉด
            else:
                task2 += floor - ground[x][y]
    
    # 3-3
    # ๋ธ”๋ก์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ > ๊ฐ€์ง€๊ณ  ์žˆ๋˜ ๋ธ”๋ก์˜ ํ•ฉ + ๋ธ”๋ก์„ ๋นผ์„œ ๊ฐ€์ ธ์˜จ ๊ฒƒ์ด๋ฉด 
    # ๋ธ”๋ก์„ ๋” ๋ชป๊ฐ€์ ธ์˜ค๋Š”๋ฐ ๊ฐ€์ ธ์™”์œผ๋ฏ€๋กœ ๋‹ค์‹œ ์ฒ˜์Œ์œผ๋กœ
    if task2 > task1 + b:
        continue
    # 3-4
    # ์‹œ๊ฐ„ ์ดˆ๋ฅผ ๊ตฌํ•˜๊ณ  ์ตœ์ € ์‹œ๊ฐ„๊ณผ ๋น„๊ต
    count = task1 * 2 + task2
    if count <= answer:
        answer = count
        idx = floor

print(answer, idx)

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์œ„์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์งœ๊ณ  ์ œ์ถœ์„ ํ–ˆ๋Š”๋ฐ ๊ณ„์† ์‹œ๊ฐ„์ดˆ๊ณผ์— ๋ง‰ํ˜€์„œ
์ฒ˜์Œ์—๋Š” sys๋ฅผ ์จ์„œ input์„ ๋ฐ”๊ฟ”์คฌ๋‹ค ์™ ๋งŒํ•˜๋ฉด ๊ทธ ๋ถ€๋ถ„์„ ํ•ด๊ฒฐ์ด ๋์–ด์„œ

๊ทผ๋ฐ ๊ทธ๋ถ€๋ถ„์œผ๋กœ๋„ ์•ˆ๋˜๊ณ  ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋– ์„œ ํ•ด๊ฒฐ์ด ์•ˆ๋˜๊ธธ๋ž˜ ์ธํ„ฐ๋„ท์— ๊ฒ€์ƒ‰์„ ํ•ด๋ณด๋‹ˆ
python์œผ๋กœ ์•ˆํ•˜๊ณ  pypy๋กœ ํ•˜๋‹ˆ๊นŒ ํ†ต๊ณผ๋๋‹ค..

๊ทธ๋ฆฌ๊ณ  ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด if์™€ elif๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฑธ if์™€ else๋กœ ๋ฐ”๊พธ๋Š”๊ฑฐ๋กœ ํ•ด๊ฒฐ์ด ๋๋‹ค๊ณ  ํ•ด์„œ ์‹œ๊ฐ„์„ ๋น„๊ตํ•ด๋ดค๋‹ค.
image

if์™€ elif else์˜ ์‹œ๊ฐ„ ์ฐจ์ด์— ๊ด€ํ•ด์„œ ์ž์„ธํ•œ ๋ถ€๋ถ„์€ stackoverflow์„ ํ†ตํ•ด ์ดํ•ดํ–ˆ์Šต๋‹ˆ๋‹ค!!

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1๋…„์ „์— ๋งŽ์ด ์‹คํŒจํ•œ ํ”์  ๋ฐœ๊ฒฌ..
image

ํ’€์—ˆ์œผ๋‹ˆ ์„ฑ์žฅํ•œ๊ฑธ๊นŒ์š”..?ใ…Žใ…Ž

์ €๋ž‘ ์ „์ฒด์ ์œผ๋กœ ๋‹ค๋ฅด๊ฒŒ ํ’€์—ˆ๋„ค์š”.

  1. ์ž…๋ ฅ ๋ฐ›์„ ๋•Œ ๊ฐ€์žฅ ๋‚ฎ์€ ๋•…, ๊ฐ€์žฅ ๋†’์€ ๋•…์„ ์ €์žฅ
  2. ํ•ด๋‹น ๋ฒ”์œ„ ๋•… ๊ณ ๋ฅด๋ฉด์„œ ์‹œ๊ฐ„ ์ฒดํฌ
    2-1. ๊ธฐ์ค€์ด ๋•…๋ณด๋‹ค ํฌ๋ฉด ๋•…ํŒŒ๊ธฐ
    2-2. ๊ธฐ์ค€์ด ๋•…๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋•…์Œ“๊ธฐ
    2-3. ์‹œ๊ฐ„ ๋ฐ˜ํ™˜(์ธ๋ฒคํ† ๋ฆฌ๊ฐ€ ์Œ์ˆ˜๋ฉด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ˆ ๋ฌดํ•œ ๋ฐ˜ํ™˜)
  3. 2๋ฒˆ์˜ ๋ฐ˜ํ™˜๋œ ์‹œ๊ฐ„์˜ ์ตœ์†Œ๊ฐ’, ๋•… ๋†’์ด ์ถœ๋ ฅ!
const val INF = Int.MAX_VALUE
fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val (n, m, b) = br.readLine().split(" ").map { it.toInt() }

    val ground = Array(n) { Array(m) { 0 } }
    var maxGround = 0
    var minGround = 256

    // ์ž…๋ ฅ๋ฐ›์„ ๋•Œ ์ตœ์ € ์ตœ๊ณ  ๋•… ๋†’์ด ์ €์žฅ
    for (i in 0 until n) {
        val line = br.readLine().split(" ").map { it.toInt() }
        for (j in 0 until m) {
            ground[i][j] = line[j]
            minGround = min(minGround, line[j])
            maxGround = max(maxGround, line[j])
        }
    }
    var minTime = INF
    var answerCondition = -1

    // ๊ฐ€์žฅ ๋‚ฎ์€ ๋•… ๋ถ€ํ„ฐ ๋†’์€ ๋•…๊นŒ์ง€ ๋ฐ˜๋ณต
    for (condition in minGround..maxGround) {
        val time = program(n, m, ground, condition, b)
        // ์‹œ๊ฐ„์ด ๋˜‘๊ฐ™์œผ๋ฉด ๋” ๋†’์€ ๋•…์œผ๋กœ ๊ณ ๋ฅด๊ธฐ
        if (minTime >= time) {
            minTime = time
            answerCondition = condition
        }
    }
    println("$minTime $answerCondition")
}

// ๋•… ๊ณ ๋ฅด๊ธฐ ์ž‘์—…
private fun program(n: Int, m: Int, ground: Array<Array<Int>>, condition: Int, b: Int): Int {
    var time = 0
    var invent = b

    for (i in 0 until n) {
        for (j in 0 until m) {
            // ๊ธฐ์ค€ ๋•…๋ณด๋‹ค ํฌ๋ฉด ๋•…ํŒŒ๊ธฐ
            if (ground[i][j] > condition) {
                val diff = ground[i][j] - condition
                time += diff * 2
                invent += diff
                continue
            }
            // ๊ธฐ์ค€ ๋•…๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋•…์Œ“๊ธฐ
            if (ground[i][j] < condition) {
                val diff = condition - ground[i][j]
                time += diff
                invent -= diff
            }
        }
    }
    // ์ธ๋ฒคํ† ๋ฆฌ๋ณด๋‹ค ๋งŽ์ด ์ผ์œผ๋ฉด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ˆ๊น ๋ฌดํ•œ์„ ๋ฐ˜ํ™˜
    if (invent < 0) {
        return INF
    }
    // ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๊ฐ€๋Šฅํ•˜๋‹ˆ๊น ์‹œ๊ฐ„ ๋ฐ˜ํ™˜
    return time
}

answer = int(1e9)
idx = 0

for floor in range(257):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0~ 256 ๋ฒ”์œ„๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š”๊ตฐ์š”!
์ €๋Š” ์ž…๋ ฅ ๋ฐ›์„ ๋•Œ ์ตœ์†Œ, ์ตœ๊ณ  ๋•… ๋†’์ด๋ฅผ ์ €์žฅํ•ด ๋†จ๋‹ค๊ฐ€ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค!

Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import sys
input = sys.stdin.readline
N,M,B = map(int, input().split())
ground = []
for _ in range(N):
    ground.extend(map(int, input().split()))

time = [0 for _ in range(257)]
level = 0
for g in range(257):
    block = B
    for i in ground :
        if i <= g : 
            time[g] += g-i
            block -= g-i
        else :
            time[g] += 2* (i-g)
            block += i-g
    if block>=0 and time[g] <= time[level] :
        level = g

print (time[level],level)

๊ฒฐ๊ตญ pypy3 ์จ์•ผ ํ•˜๋„ค์š”.. ๊ทธ๋ฆฌ๊ณ  ์ €๋Š” ์ด์ฐจ์› ๋ฐฐ์—ด์„ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์„œ ์ˆœํšŒํ•˜๊ฒŒ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants