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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

2143๋ฒˆ: ๋‘ ๋ฐฐ์—ด์˜ ํ•ฉ

์ฒซ์งธ ์ค„์— T(-1,000,000,000 ≤ T ≤ 1,000,000,000)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” n(1 ≤ n ≤ 1,000)์ด ์ฃผ์–ด์ง€๊ณ , ๊ทธ ๋‹ค์Œ ์ค„์— n๊ฐœ์˜ ์ •์ˆ˜๋กœ A[1], …, A[n]์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” m(1≤m≤1,000)์ด ์ฃผ์–ด์ง€๊ณ , ๊ทธ ๋‹ค

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ์•ž ์„œ ๋‹ค๋ฃฌ๋ถ€๋ถ„์ˆ˜์—ด์˜ ํ•ฉ 2์™€ ์œ ์‚ฌํ•œ ๋ฌธ์ œ์ด๋‹ค. ๋ถ€๋ถ„์ˆ˜์—ด์˜ ํ•ฉ 2 ๋ฌธ์ œ๋Š” ์ž…๋ ฅ๋˜๋Š” ์ˆœ์—ด์„ ์ง์ ‘ ์ ˆ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ„์–ด ์ฒ˜๋ฆฌํ•˜์—ฌ์•ผ ํ•˜์ง€๋งŒ, ํ•ด๋‹น ๋ฌธ์ œ๋Š” ์ด๋ฏธ A, B๋ผ๋Š” ๋ฐฐ์—ด์„ ๋‚˜๋ˆ„์–ด์„œ ์ž…๋ ฅ๋œ๋‹ค. ์ž…๋ ฅ๋˜๋Š” ๋ฐฐ์—ด์„ ํ•ฉ ์ค‘, T์™€ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”์ง€ ํŒ๋‹จํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

๋ถ€๋ถ„์ˆ˜์—ด์˜ ํ•ฉ 2 ๋ฌธ์ œ์™€ ๋™์ผํ•œ ๋กœ์ง์„ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์•ž์˜ ๋ฌธ์ œ ์ฒ˜๋Ÿผ ๋ฐฐ์—ด A์— ๋Œ€ํ•ด ๋ถ€๋ถ„ํ•ฉ์„ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ†ตํ•ด ์นด์šดํŠธํ•˜๊ณ , ๋ฐฐ์—ด B์—์„œ๋Š” t - B์˜ ๋ถ€๋ถ„ํ•ฉ์ด ์žˆ๋‹ค๋ฉด ์ •๋‹ต์œผ๋กœ ๋ฐ˜์˜ํ•˜๋„๋ก ํ•˜์˜€๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
from collections import defaultdict


def subtotal(lst, length, direction):
    global answer

    for i in range(length):
        cur_subtotal = 0
        for j in range(i, length):
            cur_subtotal += lst[j]
            if direction == "left":
                subtotal_a[cur_subtotal] += 1
            else:
                answer += subtotal_a[t - cur_subtotal]


if __name__ == "__main__":
    answer = 0
    subtotal_a = defaultdict(int)
    t = int(stdin.readline())
    n = int(stdin.readline())
    a = list(map(int, stdin.readline().split()))
    m = int(stdin.readline())
    b = list(map(int, stdin.readline().split()))
    subtotal(a, n, "left")
    subtotal(b, m, "right")
    print(answer)
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€