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

17-janghw0126 #186

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

17-janghw0126 #186

wants to merge 12 commits into from

Conversation

janghw0126
Copy link
Member

@janghw0126 janghw0126 commented Nov 3, 2024

πŸ”— 문제 링크

λ°±μ€€ | λžœμ„  자λ₯΄κΈ°

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

30λΆ„

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

이 λ¬Έμ œλŠ” ν˜„μž¬ 가지고 μžˆλŠ” λžœμ„ μ˜ μˆ˜λ“€κ³Ό 길이λ₯Ό μ•Œλ €μ£Όκ³ , μ΅œλŒ€ λͺ‡ μ„ΌμΉ˜μ˜ 길이λ₯Ό μž˜λΌμ•Ό μ΅œλŒ€λ‘œ μžμ‹ μ΄ μ›ν•˜λŠ” 갯수λ₯Ό 얻을 수 μžˆλŠ”μ§€ κ΅¬ν•˜λŠ” λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€.

지문을 보자마자 λžœμ„ μ„ 자λ₯΄λŠ” λ¬Έμ œμ΄λ―€λ‘œ 이뢄 탐색을 μ΄μš©ν•˜λ©΄ μ‰½κ²Œ ν’€ 수 μžˆμ„ 것이라고 μƒκ°ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

πŸ”₯ 문제 ν•΄κ²° 둜직

  1. lowλŠ” 1둜 작고, λžœμ„  쀑 κ°€μž₯ κΈ΄ μ΅œλŒ€ 길이λ₯Ό high둜 μ •ν•œ λ‹€μŒμ— 이뢄 탐색을 μ‹œμž‘ν•©λ‹ˆλ‹€.
  2. midλ₯Ό ( low + high ) // 2 둜 μ‘°μ •ν•΄μ€λ‹ˆλ‹€.
  3. κ΅¬ν•œ midλ₯Ό μ΄μš©ν•΄μ„œ λͺ¨λ“  λžœμ„ μ„ 자λ₯΄κ³ , 잘린 λžœμ„ μ˜ 갯수λ₯Ό ν•©μ‚°ν•˜μ—¬ count λ³€μˆ˜μ— μ €μž₯ν•©λ‹ˆλ‹€.
  4. λžœμ„  κ°―μˆ˜κ°€ 많이 λ‚˜μ˜€λ©΄ λžœμ„  길이가 μ§§λ‹€λŠ” κΈΈμ΄μ΄λ―€λ‘œ, 더 길게 μž˜λΌλ„ 되기 λ•Œλ¬Έμ— λžœμ„ μ˜ μ΅œμ†Œ 길이λ₯Ό mid + 1둜 μ‘°μ •ν•΄ μ€λ‹ˆλ‹€.
  5. λžœμ„  κ°―μˆ˜κ°€ μž‘κ²Œ λ‚˜μ˜€λ©΄ λžœμ„  길이가 κΈΈλ‹€λŠ” λœ»μ΄λ―€λ‘œ, 길이λ₯Ό 쀄여야 ν•˜κΈ° λ•Œλ¬Έμ— λžœμ„ μ˜ μ΅œλŒ€ 길이λ₯Ό mid - 1둜 μ‘°μ •ν•΄ μ€λ‹ˆλ‹€.
  6. μ΄λ ‡κ²Œ μ΅œμ†Œ 길이가 μ΅œλŒ€ 길이보닀 μž‘κ±°λ‚˜ 같을 λ•ŒκΉŒμ§€ μ‹€ν–‰ν•˜λ‹€κ°€ λ°˜λ³΅λ¬Έμ„ λ‚˜μ˜€κ²Œ 되면, μ΅œμ’…μ μœΌλ‘œ high에 κ°€μž₯ κΈ΄ λžœμ„ μ˜ 길이가 μ €μž₯λ©λ‹ˆλ‹€.

이λ₯Ό λ°”νƒ•μœΌλ‘œ μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

πŸ‘©β€πŸ’»μ΅œμ’… μ½”λ“œ

import sys

# μž…λ ₯ 처리
K, N = map(int, sys.stdin.readline().strip().split())
cables = [int(sys.stdin.readline().strip()) for _ in range(K)]

# 이진 탐색 λ²”μœ„ μ„€μ •
low, high = 1, max(cables)

while low <= high:
    mid = (low + high) // 2
    # mid 길이둜 자λ₯Έ λžœμ„  개수 ν•©μ‚°
    count = sum(cable // mid for cable in cables)

    if count >= N:
        low = mid + 1
    else:
        high = mid - 1

# μ΅œλŒ€ 길이 좜λ ₯
print(high)

μ‹œν—˜ λλ‚˜κ³  μ›Œλ°μ—…μœΌλ‘œ 달리고 μžˆλŠ”λ° 이뢄 탐색도 저에겐 κ°‘μžκΈ° μƒˆλ‘œμš΄ 문제처럼 λŠκ»΄μ§€λ„€μš”...!
아직 갈 길이 λ¨Ό κ°μžλ°”λΆ€μΈκ±Έ λͺΈμ†Œ λŠλ‚λ‹ˆλ‹€...πŸ₯”πŸ˜‚

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

.

Copy link
Member

@jung0115 jung0115 left a comment

Choose a reason for hiding this comment

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

λ°”λ‘œ 이뢄탐색 문제라고 νŒŒμ•…ν•˜μ‹  게 λŒ€λ‹¨ν•œ κ±° κ°™μ•„μš”...!!
저도 μ΄λ²ˆμ—” 파이썬으둜 ν’€μ–΄λ΄€λŠ”λ° 전체적인 ν’€μ΄λŠ” λΉ„μŠ·ν•˜μ§€λ§Œ

count = 0
  for line in lines :
    count += line // mid

이 뢀뢄을

count = sum(cable // mid for cable in cables)

ν˜œμ› λ‹˜μ²˜λŸΌ μ΄λ ‡κ²Œ μ€„μ—¬μ“°λŠ” 건 λ§Žμ€ μ—°μŠ΅μ΄ ν•„μš”ν•  것 κ°™μŠ΅λ‹ˆλ‹€ πŸ₯Ή μ΄λ ‡κ²Œ μ½”λ“œ 라인 수 쀄일 수 μžˆλŠ” 게 파이썬의 μž₯점이라고 μƒκ°ν•˜λŠ”λ° μ΅μˆ™ν•˜μ§€ μ•Šλ„€μš”,,,

μ•„λž˜λŠ” 풀이 μ½”λ“œμž…λ‹ˆλ‹€! 이번 μ°¨μ‹œλ„ μˆ˜κ³ ν•˜μ…¨μ–΄μš”!

# λ°±μ€€ - λžœμ„  자λ₯΄κΈ°(1654)
import sys

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

lines = []
for i in range(K) :
  lines.append(int(sys.stdin.readline()))

left = 1
right = max(lines)

while left <= right :
  mid = (left + right) // 2
  
  count = 0
  for line in lines :
    count += line // mid
  
  if(count >= N) :
    left = mid + 1
  else :
    right = mid - 1

print(right)

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