143. 두 배열의 합

아현·2021년 7월 7일
0

Algorithm

목록 보기
144/400

백준




1. Python



from collections import defaultdict
input = sys.stdin.readline

n = int(input())

a = int(input())
alist = list(map(int, input().split()))
b = int(input())
blist = list(map(int, input().split()))

adic = defaultdict(int)

answer = 0

for i in range(a):
  for j in range(i, a):
    adic[sum(alist[i:j + 1])] += 1

for i in range(b):
  for j in range(i, b):
    answer += adic[n - sum(blist[i:j + 1])]

print(answer)


bisect 사용 코드



from sys import stdin
from bisect import *


def solve():
    t = int(stdin.readline().strip())
    n = int(stdin.readline().strip())
    A = [0] + list(map(int, stdin.readline().split()))
    m = int(stdin.readline().strip())
    B = [0] + list(map(int, stdin.readline().split()))

    for i in range(1, n + 1):
        A[i] += A[i - 1]

    for i in range(1, m + 1):
        B[i] += B[i - 1]

    temp = []
    for i in range(m + 1):
        for j in range(i + 1, m + 1):
            temp.append(B[j] - B[i])
    temp.sort()

    ans = 0
    for a1 in range(n + 1):
        for a2 in range(a1 + 1, n + 1):
            now = A[a2] - A[a1]
            target = t - now
            foo = bisect_left(temp, target)
            bar = bisect_right(temp, target)
            ans += bar - foo

    return ans


print(solve())

출처

profile
For the sake of someone who studies computer science

0개의 댓글