ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

1806๋ฒˆ: ๋ถ€๋ถ„ํ•ฉ

๋ฌธ์ œ 10,000 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ ๊ธธ์ด N์งœ๋ฆฌ ์ˆ˜์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ์ˆ˜์—ด์—์„œ ์—ฐ์†๋œ ์ˆ˜๋“ค์˜ ๋ถ€๋ถ„ํ•ฉ ์ค‘์— ๊ทธ ํ•ฉ์ด S ์ด์ƒ์ด ๋˜๋Š” ๊ฒƒ ์ค‘, ๊ฐ€์žฅ ์งง์€ ๊ฒƒ์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ์•ž์„œ ๋‹ค๋ฃฌ ์ˆ˜๋“ค์˜ ํ•ฉ 2์™€ ์œ ์‚ฌํ•œ ๋ฌธ์ œ์ด๋‹ค. ๋‹ค๋ฅธ ์ ์ด ์žˆ๋‹ค๋ฉด ํ•ฉ์ด S ์ด์ƒ์ด ๋˜๋Š” ๊ฒƒ ์ค‘, ๊ฐ€์žฅ ์งง์€ ๊ฒƒ์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ˆ˜๋“ค์˜ ํ•ฉ 2์™€ ๋งˆ์ฐฌ ๊ฐ€์ง€๋กœ ์‚ฌ์ „์— ๋ˆ„์ ํ•ฉ์— ๋Œ€ํ•œ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๊ณ  ์‹œ์ž‘, ๋ ์ง€์ ์„ ๋‹ฌ๋ฆฌํ•˜์—ฌ ์ˆœ์ฐจ์ ์œผ๋กœ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•˜๋ฉด ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin


if __name__ == '__main__':
    answer = None
    start, end = 0, 1
    n, s = map(int, stdin.readline().split())
    nums = list(map(int, stdin.readline().split()))
    subtotal = [0] * (n + 1)
    for i in range(1, n + 1):
        subtotal[i] = subtotal[i - 1] + nums[i - 1]

    while start < n:
        if subtotal[end] - subtotal[start] >= s:
            answer = min(answer, end - start) if answer else end - start
            start += 1
        elif end == n:
            start += 1
        else:
            end += 1

    print(answer if answer else 0)

 

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€