This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
[2023-09-25] sumin #266 #278
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
풀이시간: 25분 | ||
|
||
<input> | ||
n: 집의 개수(2 ≤ N ≤ 200,000) | ||
c: 공유기의 개수(2 ≤ C ≤ N) | ||
집의 좌표 xi (0 ≤ xi ≤ 1,000,000,000)가 주어짐 | ||
|
||
<solution> | ||
1. 가장 인접한 두 공유기 사이의 거리를 이분탐색한다. | ||
2. 가장 인접한 집 간의 거리를 유지하면서 최대한 많은 공유기를 설치했을 때, C개 이상 설치할 수 있으면 정답의 후보가 된다. | ||
- 가능한 경우면, 거리를 더 크게 -> 더 적은 공유기 설치 가능 | ||
- 불가능한 경우면, 거리를 더 작게 -> 더 많은 공유기 설치 가능 | ||
|
||
<시간복잡도> | ||
O(nlogn) | ||
""" | ||
import sys | ||
input = sys.stdin.readline | ||
|
||
# 집, 공유기의 개수 | ||
n, c = map(int, input().split()) | ||
# 집 좌표 | ||
homes = [int(input()) for _ in range(n)] | ||
homes.sort() # 가장 인접한 집을 확인하기 위해 정렬 | ||
|
||
|
||
def possible(diff: int) -> bool: | ||
""" | ||
diff: 가장 인접한 두 공유기 사이의 거리 | ||
""" | ||
cnt = 1 # 첫 번째 집부터 설치 | ||
last = homes[0] | ||
for home in homes: # 인접한 집들 확인 | ||
if home - last >= diff: # 가장 인접한 두 공유기 사이의 거리가 diff 이상이면 | ||
cnt += 1 # 공유기 설치 개수 업데이트 | ||
last = home # 현재 집 좌표로 갱신 | ||
return cnt >= c # 최대한 많은 공유기를 설치했을 때의 개수가 c개 이상이라면 true | ||
|
||
ans = 1 | ||
left = 1 | ||
right = homes[-1] - homes[0] # 가능한 가장 먼 거리 | ||
|
||
while left <= right: | ||
mid = (left+right) // 2 # 가장 인접한 두 공유기 사이의 거리를 이분탐색 | ||
if possible(mid): # c개의 공유기가 설치 가능하다면 ans 값 업데이트 | ||
ans = mid | ||
left = mid + 1 | ||
else: # 설치가 불가능하면 오른쪽 포인터를 이동시켜 인접한 두 공유기 사이의 거리를 줄이고 다시 탐색 | ||
right = mid - 1 | ||
print(ans) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
가장 깔끔한 설명 같습니다!