๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 1292 - ์‰ฝ๊ฒŒ ํ‘ธ๋Š” ๋ฌธ์ œ

by YWTechIT 2021. 6. 17.
728x90

๐Ÿ“ ๋ฐฑ์ค€ 1292 - ์‰ฝ๊ฒŒ ํ‘ธ๋Š” ๋ฌธ์ œ

๋ฐฑ์ค€ 1292 - ์‰ฝ๊ฒŒ ํ‘ธ๋Š” ๋ฌธ์ œ


โšก๏ธ ๋‚˜์˜ ํ’€์ด

๋ฌธ์ œ ์ด๋ฆ„์€ ์‰ฝ๊ฒŒ ํ‘ธ๋Š” ๋ฌธ์ œ์˜€๋Š”๋ฐ ๋‚˜๋Š” ์–ด๋ ต๊ฒŒ ํ‘ผ ๋ฌธ์ œ์˜€๋‹ค. ๊ตฌ๊ฐ„์˜ ์‹œ์ž‘๊ณผ ๋์„ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๊ฐ„ ํ•ฉ์„ ๊ตฌํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— prefix_sum์„ ์ด์šฉํ–ˆ๋‹ค. 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 ... ์ˆ˜์—ด์„ ๋งŒ๋“ค ๋•Œ ๋” ์‰ฝ๊ฒŒ ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๋Š”๋ฐ strํ˜•์œผ๋กœ ์–ด๋ ต๊ฒŒ ํ‘ผ ๊ฒƒ ๊ฐ™๋‹ค.

 

๋‚˜์˜ ํ’€์ด

  1. arr์— 0์„ ๋„ฃ์€ ์ƒํƒœ๋กœ ์„ ์–ธํ•œ๋‹ค. (index ๊ณ ๋ ค)
  2. a, b๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋ฐ b๋Š” index๋ฅผ ๊ณ ๋ คํ•  ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋ณต๋ฌธ์˜ ๋ฒ”์œ„๋ฅผ ๋ฐ˜์œผ๋กœ ์ค„์˜€๋‹ค.
  3. cnt๋Š” 1์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉด์„œ ๋˜‘๊ฐ™์€ ๊ฐ’์ด ํ•˜๋‚˜์”ฉ ์ฆ๊ฐ€ํ•œ๋‹ค.
  4. ํ˜„์žฌ ํ•ญ๊ณผ ์ด์ „ ํ•ญ์„ ๋”ํ•ด ๋ˆ„์  ํ•ฉ์„ ๊ณ„์‚ฐํ•ด์ค€๋‹ค.
  5. arr[b] - arr[a-1]์œผ๋กœ ๊ตฌ๊ฐ„ ํ•ฉ์„ ๊ตฌํ•œ๋‹ค.

 

์ •๋‹ตํŒ์ •์„ ๋งž๊ณ  ๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ๋ฅผ ๋ดค๋Š”๋ฐ ์ฝ”๋“œ๊ธธ์ด๊ฐ€ ๋ฐ˜์œผ๋กœ ์ค„ ์ •๋„๋กœ ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€๋Š”์ง€ ๋ชฐ๋ž๋‹ค. ๐Ÿ˜… ๐Ÿ˜…

 

๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ํ’€์ด

  1. a, b์˜ ๋ฒ”์œ„๋Š” 1,000๊นŒ์ง€์˜€๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋ณต๋ฌธ์˜ ๋ฒ”์œ„๋Š” 46๊นŒ์ง€๋งŒ ๊ตฌํ•ด๋„ ๋œ๋‹ค. (len(arr) = 1036)
  2. i๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ๋นˆ arr์— i๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. ๊ฐ’์ด 1๋ถ€ํ„ฐ arr์— ๋“ค์–ด๊ฐ„๋‹ค. (j๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹˜!!)
  3. sumํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์—ˆ๋‹ค.(๊ตฌ๊ฐ„ํ•ฉ์„ ๊ตฌํ•˜์ง€ ์•Š๊ณ  ๊ธฐ์กด arr์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.)

๊ตฌ๊ฐ„ ํ•ฉ์„ ๊ตฌํ•  ๋•Œ ๋ˆ„์  ํ•ฉ์„ ๊ตฌํ•˜์ง€ ์•Š๊ณ ๋„ sum + slicing ํ•จ์ˆ˜๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ฐฐ์› ๋‹ค.

 

# ๋‚˜์˜ ์ฝ”๋“œ
a, b = map(int, input().split())

arr = [0]
cnt = 1
for i in range(1, (b+1) // 2 + 1):    # ๋ฒ”์œ„๋ฅผ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ ์„œ ์‹œ๊ฐ„ ๋‹จ์ถ•
    for j in range(cnt):
        arr.append(cnt)
    cnt += 1

for i in range(1, b+1):
    arr[i] += arr[i-1]
print(arr[b] - arr[a-1])
# ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ
a,b = map(int,input().split())

arr = [0]
for i in range(46):
    for j in range(i):
        arr.append(i)

print(sum(arr[a:b+1]))
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€