Search

2015. ์ˆ˜๋“ค์˜ ํ•ฉ 4

ํ”Œ๋žซํผ
BOJ
๋ถ„๋ฅ˜
๋‹ต ๋ณธ ๋ฌธ์ œ
์•Œ๊ณ ๋ฆฌ์ฆ˜
๋ˆ„์ ํ•ฉ
ํ•ด์‹œ ํ…Œ์ด๋ธ”
์ค‘์š”๋„
โญ๏ธโญ๏ธ
ํ‹ฐ์–ด
Gold
๋‹ค์‹œ ํ’€์–ด๋„ ํ‹€๋ฆด ๊ฒƒ ๊ฐ™์€ ๋ฌธ์ œ
Created At
2024/05/13 13:12
Updated At
2024/05/13 13:28
Writer
์ƒํƒœ
์™„๋ฃŒ

Problem

๋ฌธ์ œ

A[1], A[2], ..., A[N]์˜ N๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ฐฐ์—ด์ด ์žˆ๋‹ค. ์ด ๋ฐฐ์—ด A์˜ ๋ถ€๋ถ„ํ•ฉ์ด๋ž€ 1 โ‰ค i โ‰ค j โ‰ค N์ธ ์ •์ˆ˜ i์™€ j์— ๋Œ€ํ•ด A[i]๋ถ€ํ„ฐ A[j]๊นŒ์ง€์˜ ํ•ฉ์„ ๋งํ•œ๋‹ค.
N๊ณผ A[1], A[2], ..., A[N]์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด๋Ÿฌํ•œ Nร—(N+1)/2๊ฐœ์˜ ๋ถ€๋ถ„ํ•ฉ ์ค‘ ํ•ฉ์ด K์ธ ๊ฒƒ์ด ๋ช‡ ๊ฐœ๋‚˜ ์žˆ๋Š”์ง€๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •์ˆ˜ N๊ณผ K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 โ‰ค N โ‰ค 200,000, |K| โ‰ค 2,000,000,000) N๊ณผ K ์‚ฌ์ด์—๋Š” ๋นˆ์นธ์ด ํ•˜๋‚˜ ์žˆ๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ๋ฐฐ์—ด A๋ฅผ ์ด๋ฃจ๋Š” N๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  A[1], A[2], ..., A[N]์˜ ์ˆœ์„œ๋กœ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜์˜ ์ ˆ๋Œ“๊ฐ’์€ 10,000์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ํ•ฉ์ด K์ธ ๋ถ€๋ถ„ํ•ฉ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1

4 0 2 -2 2 -2
Plain Text
๋ณต์‚ฌ

์˜ˆ์ œ ์ถœ๋ ฅ 1

4
Plain Text
๋ณต์‚ฌ

์˜ˆ์ œ ์ž…๋ ฅ 2

6 5 1 2 3 4 5 0
Plain Text
๋ณต์‚ฌ

์˜ˆ์ œ ์ถœ๋ ฅ 2

3
Plain Text
๋ณต์‚ฌ

Code

ย #1. ์‹œ๊ฐ„์ดˆ๊ณผ ์ฝ”๋“œ

Idea

1.
๋ˆ„์ ํ•ฉ ๋ฐฐ์—ด ์ฑ„์šฐ๊ธฐ
2.
์ด์ค‘ for๋ฌธ์œผ๋กœ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ตฌ๊ฐ„ํ•ฉ ๊ตฌํ•˜๊ธฐ

ย Code

# ์‹œ๊ฐ„์ดˆ๊ณผ ์ฝ”๋“œ import sys read = sys.stdin.readline n, k = map(int, read().split()) nums = list(map(int, read().split())) psum = [0] * (n + 1) cnt = 0 for i in range(n): psum[i + 1] = psum[i] + nums[i] for i in range(n): for j in range(i, n): if psum[j + 1] - psum[i] == k: cnt += 1 print(cnt)
Python
๋ณต์‚ฌ

Commentary

N ์ด 200,000 ์ด๋ฏ€๋กœ O(N^2) ๋กœ์ง์€ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚œ๋‹ค

#2. ์ •๋‹ต์ฝ”๋“œ

Idea

๊ตฌ๊ฐ„ํ•ฉ์„ ๊ตฌํ•˜๋Š” ๊ณต์‹์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ดํ•ญ์„ ํ†ตํ•ด์„œ ์šฐ๋ณ€์— ๊ณผ๊ฑฐ ๋ˆ„์ ํ•ฉ๋งŒ์„ ๋‘”๋‹ค
(์•„๋ž˜ ์†ํ•„๊ธฐ ์ฐธ๊ณ )

ย Code

import sys read = sys.stdin.readline n, k = map(int, read().split()) nums = list(map(int, read().split())) psums = [0] * (n + 1) psum_cnt = {} ans = 0 # ๋ˆ„์ ํ•ฉ ์ƒ์„ฑ for i in range(n): psums[i + 1] = psums[i] + nums[i] # k = psum[j+1] - psum[i] # ๋ˆ„์ ํ•ฉ ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ psum - k ๊ฐ€ psum_cnt ์— ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๊ณ  # ์žˆ์œผ๋ฉด (psum-k) ์˜ ๊ฐœ์ˆ˜๋งŒํผ ๋”ํ•ด์ค€๋‹ค # ๊ทธ ํ›„ psum_cnt ์˜ psum ๊ฐ’์„ 1 ์ฆ๊ฐ€์‹œํ‚จ๋‹ค (๊ฐœ์ˆ˜ 1 ์ฆ๊ฐ€) for psum in psums: if psum - k in psum_cnt: ans += psum_cnt[psum - k] psum_cnt[psum] = psum_cnt.get(psum, 0) + 1 print(ans)
Python
๋ณต์‚ฌ

Solution

Commentary

โ€ข
๊ตฌ๊ฐ„ํ•ฉ์„ 2์ค‘ for๋ฌธ์ด ์•„๋‹Œ for ๋ฌธ ํ•˜๋‚˜๋กœ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ์•„์ด๋””์–ด๋ฅผ ์ƒ๊ฐํ•˜๋Š”๊ฒŒ ํ•ต์‹ฌ์ด์˜€์Œ
โ—ฆ
ํ˜„์žฌ๋ˆ„์ ํ•ฉ(psum) - k ๊ฐ€ ๊ณผ๊ฑฐ๋ˆ„์ ํ•ฉ ๋”•์…”๋„ˆ๋ฆฌ(prefix_cnt) ์— ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์•„์ด๋””์–ด
โ€ข
n,k ๊ฐ€ ๋งค์šฐ ํฌ๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์ˆœ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•„๋‹ˆ๋ผ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ’ ์กฐํšŒ ์‹œ๊ฐ„์„ O(1) ๋กœ ๋‹จ์ถ•
โ€ข
์•„์ด๋””์–ด ๋ฐœ์ƒ์ด ๋งค์šฐ๋งค์šฐ ์–ด๋ ค์› ๊ณ  ํ’€์ด๋ฅผ ์ฒ˜์Œ์— ๋ณด๊ณ ๋„ ์ดํ•ดํ•˜๊ธฐ๊นŒ์ง€๋„ ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค