백준(Python) 2805, 2110, 1300

오성인·2023년 3월 18일
0

알고리즘

목록 보기
12/18
post-custom-banner

https://www.acmicpc.net/problem/2805

import sys
n, target = map(int, sys.stdin.readline().split())
tree = list(map(int, sys.stdin.readline().split()))

start, end = 1, max(tree)

while start <= end:
    mid = (start + end) // 2
    cnt = 0
    for i in tree:
        if i >= mid:
            cnt += i - mid
        
    if cnt >= target:
        start = mid + 1
    else:
        end = mid - 1
print(end)

https://www.acmicpc.net/problem/2110

import sys 

input = sys.stdin.readline

n, c = map(int, input().split())
house = []
for _ in range(n):
    house.append(int(input()))
house.sort()
answer = 0

start, end = 1, house[-1]-house[0]
while start <= end:
    mid = (start + end) // 2
    cnt = 1
    current = house[0]
    for i in range(n):
        if house[i] - current >= mid:
            cnt += 1
            current = house[i]
    if cnt >= c:
        answer = max(mid, answer)
        start = mid + 1
    else:
        end = mid -1

print(answer)
     

https://www.acmicpc.net/problem/1300

n = int(input())
k = int(input())

start, end = 1, n**2
result = 0

if n**2 == k:
    print(k)
    # k가 n의 제곱이면 당연히 맨 끝자리 수이므로 k를 출력한다.
else:
    while(start < end):
        mid = (start+end)//2

        c = 0
        # mid보다 작거나 같은 숫자 계산
        for i in range(1,n+1):
            c += min(mid // i, n)
        
        if c >= k:
            end = mid
            result = mid
        elif c < k:
            start = mid+1
    print(result)
profile
기여하는 개발자
post-custom-banner

0개의 댓글