From 32ec66e6a99722c3de20a10a5033f5ff0e4b05d5 Mon Sep 17 00:00:00 2001 From: sm4640 Date: Thu, 2 Apr 2026 17:19:13 +0900 Subject: [PATCH] =?UTF-8?q?1654-s2=20=EC=84=B1=EA=B3=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1654-s2.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 1654-s2.py diff --git a/1654-s2.py b/1654-s2.py new file mode 100644 index 0000000..7f97b8a --- /dev/null +++ b/1654-s2.py @@ -0,0 +1,47 @@ +# 랜선 자르기 + +import sys + +input = sys.stdin.readline + +def binary_search(l, r, lan_lst, target): + result = 0 + while l <= r: + count = 0 + mid = (l+r)//2 + for lan in lan_lst: + count += (lan // mid) + + if count >= target: + l = mid+1 + result = mid + else: + r = mid-1 + return result + +def solution(): + k, n = map(int, input().rstrip().split()) + + max_len = 0 + lan_lst = [0] * k + for i in range(k): + lan_lst[i] = int(input().rstrip()) + max_len = max(max_len, lan_lst[i]) + + result = binary_search(l=1, r=max_len+1, lan_lst=lan_lst, target=n) + print(result) + + return + +solution() + +""" +걸린 시간: 1시간 + +시간 복잡도: 1부터 가장 큰 랜선 길이까지의 범위에서 이분탐색을 진행하므로 O(log(2^31-1))이고, 이분탐색을 할 때마다 k개의 랜선을 잘라보기 때문에 O(klogn)이다. + +해설: 최대 길이를 찾는 것이기 때문에 주어진 랜선 중 가장 긴 길이부터 아래로 내려가면서 n개가 나오는 첫 지점을 찾으면 된다. +근데 이렇게 하면 랜선의 길이가 최대 2^31-1이므로 너무 오래 걸린다. +마침 수직선이기 때문에 이분탐색을 하면 되겠다는 생각을 했고, 이렇게 최적화 문제를 결정문제로 바꿔서 푸는 것을 파라메트릭 서치라고 한다. +이분 탐색 구현은 count가 n보다 크면 일단 답 후보로 넣어놓고, l을 mid+1로 옮겼고, 반대의 상황일 때는 답 후보로 넣지 않고, r을 mid-1로 했다. +""" \ No newline at end of file