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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

2003๋ฒˆ: ์ˆ˜๋“ค์˜ ํ•ฉ 2

์ฒซ์งธ ์ค„์— N(1≤N≤10,000), M(1≤M≤300,000,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” A[1], A[2], …, A[N]์ด ๊ณต๋ฐฑ์œผ๋กœ ๋ถ„๋ฆฌ๋˜์–ด ์ฃผ์–ด์ง„๋‹ค. ๊ฐ๊ฐ์˜ A[x]๋Š” 30,000์„ ๋„˜์ง€ ์•Š๋Š” ์ž์—ฐ์ˆ˜์ด๋‹ค.

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 N ํฌ๊ธฐ์˜ ์ˆ˜์—ด์ด ์ฃผ์–ด์งˆ ๋•Œ, ์ˆ˜์—ด์˜ ๋ถ€๋ถ„ ํ•ฉ ์ค‘ M์„ ๋งŒ์กฑํ•˜๋Š” ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋ฌธ์ œ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ํ’€๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฏธ๋ฆฌ 1 ~ N๊นŒ์ง€์˜ ๋ˆ„์ ํ•ฉ์„ ๊ณ„์‚ฐํ•œ ํ›„์— ์‹œ์ž‘, ๋ ์ง€์ ์„ ๋‹ฌ๋ฆฌํ•˜๋ฉฐ ๋ถ€๋ถ„ํ•ฉ์„ ๊ณ„์‚ฐํ•˜๋ฉด ๋œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ 1์„ ๊ธฐ์ค€์œผ๋กœ ํ•ฉ์„ ๋ˆ„์ ์‹œํ‚ค๋ฉด [0, 1, 2, 3, 4]์™€ ๊ฐ™์ด ํ•ฉ์ด ๋ˆ„์ ๋œ๋‹ค. ์ด๋ฅผ ์‹œ์ž‘ ๋ ์ง€์ ์„ 0, 1๋กœ ๋‘๊ณ  ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฆ๊ฐ€์‹œ์ผœ ๊ฐ€๋ฉฐ ํƒ์ƒ‰์„ ํ•˜๋ฉด, ์ „์ฒด ๋ถ€๋ถ„ํ•ฉ์— ๋Œ€ํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

  • start : 0, end :1
    • ๋ˆ„์ ํ•ฉ[end] - ๋ˆ„์ ํ•ฉ[start] != M ์ด๋ฏ€๋กœ end๋ฅผ 1 ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
    • start : 0, end : 2
      • ๋ˆ„์ ํ•ฉ[end] - ๋ˆ„์ ํ•ฉ[start] == M ์ด๋ฏ€๋กœ ํ•˜๋‚˜์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.
  • ์ด์™€ ๊ฐ™์ด start์— ๋”ฐ๋ผ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋ฉด
    • M์„ ๋งŒ์กฑํ•˜๋Š” start end๋Š” [[0, 2], [1, 3], [2, 4]] ์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 ๋‹จ์ˆœํžˆ ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๊ฐ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํŒ๋‹จํ•˜์—ฌ๋„ ๋˜์ง€๋งŒ, ํŒŒ์ด์ฌ์œผ๋กœ๋Š” ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin


if __name__ == '__main__':
    answer = 0
    start, end = 0, 1
    n, m = 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 and end <= n:
        cur_subtotal = subtotal[end] - subtotal[start]
        if cur_subtotal == m:
            answer += 1
            start += 1
        if cur_subtotal < m:
            end += 1
        elif cur_subtotal > m:
            start += 1

    print(answer)

 

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