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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

7453๋ฒˆ: ํ•ฉ์ด 0์ธ ๋„ค ์ •์ˆ˜

๋ฌธ์ œ ์ •์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ ํฌ๊ธฐ๊ฐ€ ๊ฐ™์€ ๋ฐฐ์—ด A, B, C, D๊ฐ€ ์žˆ๋‹ค. A[a], B[b], C[c], D[d]์˜ ํ•ฉ์ด 0์ธ (a, b, c, d) ์Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์ž…๋ ฅ ์ฒซ์งธ ์ค„์— ๋ฐฐ์—ด์˜ ํฌ๊ธฐ n (1 ≤ n ≤ 4000)์ด ์ฃผ๏ฟฝ๏ฟฝ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ์•ž ์„œ ๋‹ค๋ฃฌ ๋ถ€๋ถ„์ˆ˜์—ด์˜ ํ•ฉ 2, ๋‘ ๋ฐฐ์—ด์˜ ํ•ฉ ํ•ฉ๊ณผ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ํ‘ธ๋Š” ๋ฌธ์ œ์ด๋‹ค. ํ•œ ์ค„์— A, B, C, D 4 ์Œ์˜ ๊ฐ’์ด ์žˆ๊ณ  N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ์—ฌ๋Ÿฌ ์Œ์˜ ์ •์ˆ˜๋“ค์ด ์ฃผ์–ด์ง„๋‹ค. ์•ž์˜ ๋ฌธ์ œ๋“ค๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•œ ๋ฒˆ์— ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์€ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๋”ฐ๋ผ์„œ A, B์— ๋Œ€ํ•œ ๋ถ€๋ถ„ํ•ฉ์— ๋Œ€ํ•œ ์นด์šดํŠธ๋ฅผ ์ง„ํ–‰ํ•œ ํ›„, 0 - C, D์˜ ๋ถ€๋ถ„ํ•ฉ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ •๋‹ต์œผ๋กœ ๋ฐ˜์˜ํ•˜๋ฉด ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
from collections import defaultdict


def subtotal(lst, select):
    global answer

    for i in lst[0]:
        for j in lst[1]:
            if select == "ab":
                subtotal_a_b[i + j] += 1
            else:
                answer += subtotal_a_b[0 - (i + j)]


if __name__ == "__main__":
    answer = 0
    subtotal_a_b = defaultdict(int)
    n = int(stdin.readline())
    nums = [list(map(int, stdin.readline().split())) for _ in range(n)]
    nums = list(map(list, zip(*nums)))
    subtotal(nums[:2], "ab")
    subtotal(nums[2:], "cd")
    print(answer)

 ์ž…๋ ฅ๋œ ๊ฐ’์„ [A, B], [C, D]๋กœ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์œ„ํ•ด ํ–‰์—ด์„ ์ „์น˜์‹œ์ผฐ๋‹ค. Numpy ์—†์ด ํ–‰์—ด์„ ์ „์น˜์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ๋Š” `map`๊ณผ `zip`์„ ํ™œ์šฉํ•˜๋ฉด `list(map(list, zip(*์ „์น˜ํ•  ํ–‰์—ด)))`๊ณผ ๊ฐ™์ด ํ•œ ์ค„๋กœ ์ „์น˜์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

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