๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 13164 - ํ–‰๋ณต ์œ ์น˜์›

by HandHand 2021. 3. 14.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 13164๋ฒˆ

ํ’€์ด ๊ณผ์ •

๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ๊ฐ€ ํ•ญ์ƒ ๊ทธ๋ ‡๋“ฏ ํ’€์ด๋Š” ์ •๋ง ๊ฐ„๋‹จํ•œ๋ฐ ์•„์ด๋””์–ด๋ฅผ ์ƒ๊ฐํ•˜๋Š”๊ฒŒ ์–ด๋ ค์šด ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

K ๊ฐœ์˜ ๊ทธ๋ฃน์„ ๋งŒ๋“ค์–ด์•ผํ•˜๊ณ  ๋ชจ๋“  ํ‚ค ์ •๋ณด๋Š” ์–‘์ˆ˜๋ผ๋Š” ์ ์„ ์ด์šฉํ•ด์„œ
์ฒ˜์Œ์—๋Š” 2๋ช… ์ด์ƒ์˜ ๊ทธ๋ฃน์˜ ์ˆ˜๋ฅผ ์ตœ์†Œํ™” ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์ ‘๊ทผํ–ˆ์Šต๋‹ˆ๋‹ค.
์—ฌ๊ธฐ์„œ ๊ทธ๋ฃน์˜ ์ธ์›์ˆ˜๋Š” ๋ฌด์กฐ๊ฑด ์ ์€๊ฒŒ ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ 2์ธ ๊ทธ๋ฃน ์˜ ์ˆ˜๋ฅผ ์ตœ์†Œํ™” ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ๊ณ ๋ฏผํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฐ๋ฐ ์ด๋ ‡๊ฒŒ ์ ‘๊ทผํ•˜๋‹ˆ๊นŒ ์ตœ์ ํ•ด๋ฅผ ๊ณ ๋ฅด๋Š” ๋ช…ํ™•ํ•œ ๊ธฐ์ค€์„ ์žก๊ธฐ๊ฐ€ ์–ด๋ ค์› ๊ณ  ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ๋Š” ๋ชจ์ˆœ์ด ์ƒ๊ธฐ๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๊ฒฐ๊ตญ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ–ˆ๋Š”๋ฐ ํƒ€ ๋ธ”๋กœ๊ทธ์˜ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฃน์˜ ์ˆ˜๊ฐ€ K ์ผ ๋•Œ ๊ทธ๋ฃน์˜ ๊ฒฝ๊ณ„๋Š” K - 1 ๊ฐœ ๋งŒํผ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
์—ฌ๊ธฐ์„œ ์ด ๊ฒฝ๊ณ„ ๊ฐ’๋“ค์„ ์ตœ๋Œ€ํ™”ํ•ด์„œ ๊ทธ๋ฃน์„ ๋งŒ๋“ค๋ฉด ๊ทธ๋ฃน ๋‚ด๋ถ€์˜ ์ตœ์†Œ ๋ฐ ์ตœ๋Œ€์˜ ์ฐจ์ด ๊ฐ’์€ ์ตœ์ ํ•ด๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•œ ๋‹ค์Œ ์ƒ์œ„ K - 1 ๊ฐœ์˜ ์ธ์ ‘ ์ฐจ์ด ๊ฐ’์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€์˜ ํ•ฉ์ด ์šฐ๋ฆฌ๊ฐ€ ์ฐพ๋Š” ๋‹ต์ด ๋ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys


N, K = list(map(int, sys.stdin.readline().split()))
children = list(map(int, sys.stdin.readline().split()))


def solution():
    diff = []
    for i in range(len(children) - 1):
        diff.append(children[i + 1] - children[i])

    diff.sort(reverse=True)
    answer = sum(diff[K - 1:])

    return answer


print(solution())
๋ฐ˜์‘ํ˜•

'๐Ÿƒ algorithm > boj' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ 14588 - Line Friends (Small)  (0) 2021.03.14
BOJ 14925 - ๋ชฉ์žฅ ๊ฑด์„คํ•˜๊ธฐ  (0) 2021.03.14
BOJ 3109 - ๋นต์ง‘  (0) 2021.03.14
BOJ 14620 - ๊ฝƒ๊ธธ  (0) 2021.03.14
BOJ 2688 - ์ค„์–ด๋“ค์ง€ ์•Š์•„  (0) 2021.03.14

๐Ÿ’ฌ ๋Œ“๊ธ€