ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 2304 ์ฐฝ๊ณ  ๋‹ค๊ฐํ˜• with Python
    PS 2021. 11. 29. 21:14
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 2304 ์ฐฝ๊ณ  ๋‹ค๊ฐํ˜•

    ๐Ÿ’ก ์กฐ๊ฑด

    1. ๊ธฐ๋‘ฅ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ (1 <= N <= 1000)
      ๊ฐ ๊ธฐ๋‘ฅ์˜ ์™ผ์ชฝ ๋ฉด์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ (1 <= L <= 1000)
      ๊ฐ ๊ธฐ๋‘ฅ์˜ ๋†’์ด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ (1 <= H <= 1000)

    2. ์ฐฝ๊ณ  ๋‹ค๊ฐํ˜•์˜ ๋ฉด์ ์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ

    3. ๋ชจ๋“  ๊ธฐ๋‘ฅ์ด ๋“ค์–ด๊ฐ€๋Š” ์ฐฝ๊ณ ๋ฅผ ์ง€์œผ๋ ค๊ณ  ํ•  ๋•Œ, ์ง€๋ถ•์ด ๋  ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

    • ์ง€๋ถ•์€ ์ˆ˜ํ‰ ๋ถ€๋ถ„๊ณผ ์ˆ˜์ง ๋ถ€๋ถ„์œผ๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ, ๋ชจ๋‘ ์—ฐ๊ฒฐ๋˜์–ด์•ผ ํ•œ๋‹ค.
    • ์ง€๋ถ•์˜ ์ˆ˜ํ‰ ๋ถ€๋ถ„์€ ๋ฐ˜๋“œ์‹œ ์–ด๋–ค ๊ธฐ๋‘ฅ์˜ ์œ—๋ฉด๊ณผ ๋‹ฟ์•„์•ผ ํ•œ๋‹ค.
    • ์ง€๋ถ•์˜ ์ˆ˜์ง ๋ถ€๋ถ„์€ ๋ฐ˜๋“œ์‹œ ์–ด๋–ค ๊ธฐ๋‘ฅ์˜ ์˜†๋ฉด๊ณผ ๋‹ฟ์•„์•ผ ํ•œ๋‹ค.
    • ์ง€๋ถ•์˜ ๊ฐ€์žฅ์ž๋ฆฌ๋Š” ๋•…์— ๋‹ฟ์•„์•ผ ํ•œ๋‹ค.
    • ๋น„๊ฐ€ ์˜ฌ ๋•Œ ๋ฌผ์ด ๊ณ ์ด์ง€ ์•Š๋„๋ก ์ง€๋ถ•์˜ ์–ด๋–ค ๋ถ€๋ถ„๋„ ์˜ค๋ชฉํ•˜๊ฒŒ ๋“ค์–ด๊ฐ„ ๋ถ€๋ถ„์ด ์—†์–ด์•ผ ํ•œ๋‹ค.
    1. ์ž๋ฃŒ๊ตฌ์กฐ, ๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜์œ ํ˜•์˜ ๋ฌธ์ œ

    ๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

    from sys import stdin
    
    n = int(stdin.readline())
    max_pillar = -1e9
    idx = 0
    arr = [[] for _ in range(1001)]
    res = 0
    last_idx = 0
    
    for _ in range(n):
        a, b = map(int, stdin.readline().split())
        if max_pillar < b:
            max_pillar = b
            idx = a
    
        arr[a].append(b)
        last_idx = max(last_idx, a)
    
    part1 = arr[:idx+1]
    part2 = arr[idx+1:last_idx+1]
    
    now = 0
    for i in part1:
        if not i:
            res += now
        else:
            if now > i[0]:
                res += now
            else:
                now = i[0]
                res += now
    
    part2.reverse()
    now = 0
    for i in part2:
        if not i:
            res += now
        else:
            if now > i[0]:
                res += now
            else:
                now = i[0]
                res += now
    
    print(res)

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    7
    2 4
    11 4
    15 8
    4 6
    5 3
    8 10
    13 6

    ์‹คํ–‰๊ฒฐ๊ณผ

    98

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. ๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด ๋งค์šฐ ์ข‹์€ ๋ฌธ์ œ์ด๋‹ค. ๋ฌธ์ œ์—์„œ ์ œ์‹œ๋œ ๊ทธ๋ฆผ์—์„œ๋„ ๋ณด์ด๋“ฏ, ๊ธฐ๋‘ฅ๋“ค ์ค‘์—์„œ ๊ฐ€์žฅ ๋†’์ด ๊ฐ’์ด ํฐ ๊ธฐ๋‘ฅ์ด ์žˆ๋‹ค.
      ๊ทธ ๊ฐ€์žฅ ๋†’์ด๊ฐ€ ๋†’์€ ๊ธฐ๋‘ฅ์„ ์ค‘์‹ฌ์œผ๋กœ ๋‚˜๋ˆ„์–ด์„œ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฉด์ ์„ ๊ตฌํ•˜๊ณ , ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ๋ฉด์ ์„ ๊ตฌํ•ด ๋”ํ•œ ํ›„ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

    2. index๋ฅผ ์ž˜ ๊ตฌ๋ณ„ํ•ด์„œ ์จ์•ผ ํ—ท๊ธธ๋ฆฌ์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.

    ๐Ÿ’พ ๋Š๋‚€์ 

    1. ํ‘ผ์ง€ ์กฐ๊ธˆ ์˜ค๋ž˜๋œ ๋ฌธ์ œ์ด์ง€๋งŒ, ๋ฐ”๋กœ ๋ฐ”์ž๋งˆ์ž ํ’€์ด๊ฐ€ ์ƒ๊ฐ์ด ๋‚ฌ๋‹ค.
    2. ์ง€๊ธˆ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์ž‘์„ฑํ•œ๋‹ค๋ฉด ๋” ๊ฐ„๊ฒฐํ•˜๊ณ  ์ด์˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.