Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21-H0ngJu #208

Merged
merged 1 commit into from
Jun 29, 2024
Merged

21-H0ngJu #208

merged 1 commit into from
Jun 29, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented Jun 7, 2024

πŸ”— 문제 링크

행볡 μœ μΉ˜μ›

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

30M

✨ μˆ˜λ„ μ½”λ“œ

image

문제 μš”μ•½
Nλͺ…μ˜ 원생에 λŒ€ν•΄μ„œ K개의 쑰둜 λ‚˜λˆ„μ—ˆμ„ λ•Œ ν‹°μ…”μΈ  μ œμž‘ λΉ„μš©μ΄ κ°€μž₯ 적게 듀도둝 ν•˜λΌ@! 단, μ‘°λŠ” μ΄μ›ƒν•œ 아이듀끼리 κ΅¬μ„±ν•΄μ•Όν•œλ‹€. (μ œμž‘ λΉ„μš©μ€ 같은 μ‘°μ—μ„œ κ°€μž₯ 큰 아이 - κ°€μž₯ μž‘μ€ 아이이닀.)



이 문제λ₯Ό ν‘ΈλŠ” ν‚€λŠ”
쑰에 λˆ„κ°€ μžˆλŠ”μ§€λŠ” κ³ λ €ν•  ν•„μš”κ°€ μ—†λ‹€ 인 것 κ°™μŠ΅λ‹ˆλ‹€.


λͺ‡λͺ…이 쑰인지, λˆ„κ°€ μ‘°μΈμ§€λŠ” κ³ λ €ν•  ν•„μš” 없이,

킀큰 아이 - ν‚€ μž‘μ€ 아이λ₯Ό 뺐을 λ•Œ κ°€μž₯ 큰 K-1개λ₯Ό μ œμ™Έμ‹œμΌœμ£Όλ©΄ μžμ—°μŠ€λ ˆ μ‘°κ°€ λ‚˜λ‰˜κ²Œ λ©λ‹ˆλ‹€.



예λ₯Ό λ“€μ–΄,
N = 5, K = 3이고

μ›μƒλ“€μ˜ ν‚€λŠ”
1 3 5 6 10 이라고 κ°€μ •ν•΄λ΄…μ‹œλ‹€.

κ·Έλ ‡λ‹€λ©΄ μ΄μ›ƒν•œ μ•„μ΄λ“€λΌλ¦¬μ˜ ν‚€ μ°¨μ΄λŠ”
2 2 1 4κ°€ λ©λ‹ˆλ‹€.

이것을 μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬ν•˜λ©΄

4 2 2 1이 되고,

μ‘°λ₯Ό λ‚˜λˆ„λ©΄ K-1인 2개λ₯Ό λΉΌλ©΄ λ˜λ―€λ‘œ

2 + 1 = 3이 μ΅œμ†Œ λΉ„μš©μ΄ λ©λ‹ˆλ‹€.



μˆ˜λ„μ½”λ“œ

  1. μ΄μ›ƒν•œ μ•„μ΄λ“€λΌλ¦¬μ˜ ν‚€ 차이λ₯Ό κ΅¬ν•œλ‹€
  2. ν‚€ 차이λ₯Ό λ‚΄λ¦Όμ°¨μˆœμœΌλ‘œ μ •λ ¬
  3. κ°€μž₯ 차이가 큰 K-1개λ₯Ό λΉΌκ³  더 ν•΄μ£Όλ©΄ μ΅œμ†Œ λΉ„μš©μ΄ λ‚˜μ˜¨λ‹€.

μ €λŠ” κ΅¬ν˜„μ΄λΌκ³  μƒκ°ν•˜κ³  ν’€μ—ˆλŠ”λ°,
μ•Œκ³ λ¦¬μ¦˜ λΆ„λ₯˜λ₯Ό λ³΄λ‹ˆκΉŒ κ·Έλ¦¬λ””λ„€μš€ πŸ€”

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from heapq import *
import sys

def input(): return sys.stdin.readline().rstrip()

N, M = map(int, input().split())
children = list(map(int, input().split()))

gap = []
prev = 0
for idx, child in enumerate(children):
    if idx == 0:
        prev = child
        continue

    heappush(gap,(-1*abs(child-prev), idx))
    prev = child

link = [N]
for _ in range(M-1):
    value, idx = heappop(gap)
    heappush(link, idx)

answer = 0
prev = 0
while link:
    now_idx = heappop(link)

    if prev == now_idx-1:
        prev = now_idx
        continue

    else:
        answer += children[now_idx-1] - children[prev]
        prev = now_idx

print(answer)

ν—‰ μ „ 바보같이 κ°€μž₯ gap이 큰 아이λ₯Ό λΉΌμ•Όν•˜κ² λ‹€λŠ” 것을 μš°μ„ μˆœμœ„νλ‘œ κ΅¬ν˜„μ„ ν–ˆλ„€μš”...

κ·Έλƒ₯ μ •λ ¬λ‘œ ν–ˆμœΌλ©΄ μ™„μ „ νŽΈν–ˆμ„ν…λ°............................γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… 




κ·Έλž˜λ„ κ·Έλž˜λ„... 원큐!!!!!!!!!!!!

image
image

Comment on lines +12 to +17
diff.sort(reverse=True)

total_diff = sum(diff)

for i in range(K-1):
total_diff -= diff[i]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와 κ·Έλƒ₯ diff둜만 해도 λ˜λ„€μš”... κ°œλ˜‘λ˜‘..

@H0ngJu H0ngJu merged commit 3d6a6b1 into main Jun 29, 2024
12 checks passed
@H0ngJu H0ngJu deleted the 21-H0ngJu branch June 29, 2024 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants