DevYoon

[๋ฐฑ์ค€] 16434. ๋“œ๋ž˜๊ณค ์•ค ๋˜์ „ (Python) ๋ณธ๋ฌธ

PS/Baekjoon

[๋ฐฑ์ค€] 16434. ๋“œ๋ž˜๊ณค ์•ค ๋˜์ „ (Python)

gimewn 2022. 8. 24. 00:05

link ๐Ÿ”— https://www.acmicpc.net/problem/16434

 

16434๋ฒˆ: ๋“œ๋ž˜๊ณค ์•ค ๋˜์ „

์ฒซ ๋ฒˆ์งธ ์ค„์— ๋ฐฉ์˜ ๊ฐœ์ˆ˜ N (1 ≤ N  ≤ 123,456) ๊ณผ ์šฉ์‚ฌ์˜ ์ดˆ๊ธฐ ๊ณต๊ฒฉ๋ ฅ HATK (1 ≤ HATK  ≤ 1,000,000) ๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. i+1๋ฒˆ์งธ ์ค„์—” i๋ฒˆ์งธ ๋ฐฉ์˜ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์„ธ๊ฐœ์˜ ์ •์ˆ˜ ti, ai, hi (ti ∈ {1, 2}, 1

www.acmicpc.net

 

๐Ÿ“Œ ๋ชฌ์Šคํ„ฐ ๋ฐฉ์ผ ๊ฒฝ์šฐ์™€ ํฌ์…˜ ๋ฐฉ์ผ ๊ฒฝ์šฐ๋ฅผ ๋‚˜๋ˆ„์–ด ์ƒ๊ฐํ•ด๋ณด์•˜๋‹ค.

๐Ÿ“Œ ๋ชฌ์Šคํ„ฐ ๋ฐฉ์ผ ๊ฒฝ์šฐ, ์šฉ์‚ฌ๊ฐ€ n๋ฒˆ ๊ณต๊ฒฉํ•˜๋ฉด ๋“œ๋ž˜๊ณค์€ n-1๋ฒˆ ๊ณต๊ฒฉํ•œ๋‹ค๋Š” ์ ์—์„œ ์ฐฉ์•ˆํ•˜์—ฌ

       1๏ธโƒฃ ๋ชฌ์Šคํ„ฐ์˜ ์ƒ๋ช…๋ ฅ์ด ์šฉ์‚ฌ์˜ ๊ณต๊ฒฉ๋ ฅ์œผ๋กœ ๋”ฑ ๋‚˜๋ˆ„์–ด์งˆ ๋•Œ๋Š” 1์„ ๋นผ์ค€๋‹ค

       2๏ธโƒฃ ๋ชฌ์Šคํ„ฐ์˜ ์ƒ๋ช…๋ ฅ์ด ์šฉ์‚ฌ์˜ ๊ณต๊ฒฉ๋ ฅ์œผ๋กœ ๋”ฑ ๋‚˜๋ˆ„์–ด์ง€์ง€ ์•Š์„ ๋•Œ๋Š” 1์„ ๋นผ์ฃผ์ง€ ์•Š๋Š”๋‹ค.

๐Ÿ“Œ ์œ„์—์„œ ๊ตฌํ•ด์ง„ ๊ฐ’์— ๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ๋ ฅ์„ ๊ณฑํ•˜๋ฉด ๋ฐ๋ฏธ์ง€๊ฐ€ ๋‚˜์˜ค๊ณ , ์ด๋ฅผ ์šฉ์‚ฌ์˜ ํ˜„์žฌ ์ƒ๋ช…๋ ฅ์— -๋ฅผ ๋ถ™์—ฌ ๋”ํ•ด์ค€๋‹ค.

๐Ÿ“Œ ํฌ์…˜ ๋ฐฉ์˜ ๊ฒฝ์šฐ, ํšŒ๋ณต๋˜๋Š” ์ƒ๋ช…๋ ฅ์„ ์šฉ์‚ฌ์˜ ํ˜„์žฌ ์ƒ๋ช…๋ ฅ์— ๋”ํ•ด์ค€๋‹ค.

๐Ÿ“Œ ์ด ๋•Œ, ํšŒ๋ณต๋œ ์ƒ๋ช…๋ ฅ์ด ์ตœ๋Œ€ ์ƒ๋ช…๋ ฅ๋ณด๋‹ค ํฌ๋ฉด ์ตœ๋Œ€ ์ƒ๋ช…๋ ฅ์ด ๋œ๋‹ค๋Š” ์กฐ๊ฑด์ด ์žˆ์œผ๋ฏ€๋กœ, ์ดํ›„ ์ƒ๋ช…๋ ฅ์ด +์ผ ๊ฒฝ์šฐ 0์œผ๋กœ ๋งŒ๋“ค์–ด ์ค€๋‹ค.

๐Ÿ“Œ ์‚ด์•„์žˆ์–ด์•ผ ํ•˜๋ฏ€๋กœ +1์„ ๋”ํ•ด ์ถœ๋ ฅํ•ด์ค€๋‹ค!

 

import sys

N, A = map(int, sys.stdin.readline().split())
res = 0
damage = 0
now = 0

for idx in range(N):
    t, a, h = map(int, sys.stdin.readline().split())
    if t == 1: # ๋ชฌ์Šคํ„ฐ๋ฐฉ
        if h % A == 0: # ๋”ฑ ๋‚˜๋ˆ  ๋–จ์–ด์ง€๋ฉด
            damage = -1*(((h//A)-1)*a) # 1 ๋นผ์ฃผ๊ธฐ (์šฉ์‚ฌ๊ฐ€ n๋ฒˆ ๊ณต๊ฒฉํ•˜๋ฉด ๋ชฌ์Šคํ„ฐ๋Š” n-1๋ฒˆ ๊ณต๊ฒฉํ•˜๋ฏ€๋กœ)
        else:
            damage = -1*((h//A)*a)

    elif t == 2: # ํฌ์…˜ ๋ฐฉ์ด๋ฉด
        A += a # ๊ณต๊ฒฉ๋ ฅ ์ฆ๊ฐ€

    if t == 1:
        now += damage
    elif t == 2:
        now += h

    if now > 0: # ํšŒ๋ณต๋œ ์ƒ๋ช…๋ ฅ์ด ์ตœ๋Œ€ ์ƒ๋ช…๋ ฅ๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ ํ˜„์žฌ ์ƒ๋ช…๋ ฅ == ์ตœ๋Œ€ ์ƒ๋ช…๋ ฅ
        now = 0

    res = max(res, abs(now)) # ํ•„์š”ํ•œ ์ƒ๋ช…๋ ฅ ๋น„๊ต

print(res+1) # ์‚ด์•„ ์žˆ์–ด์•ผ ํ•˜๋ฏ€๋กœ +1