Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

:)

[백준] 놀이공원 본문

Algorithm(python)

[백준] 놀이공원

mihee 2022. 11. 21. 11:03

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

 

1561번: 놀이 공원

첫째 줄에 N(1 ≤ N ≤ 2,000,000,000)과 M(1 ≤ M ≤ 10,000)이 빈칸을 사이에 두고 주어진다. 둘째 줄에는 각 놀이기구의 운행 시간을 나타내는 M개의 자연수가 순서대로 주어진다. 운행 시간은 1 이상 30

www.acmicpc.net

이분탐색

import sys

def solution(n,m,time):
    
    def get_prev_time_list(time,mid):
        prev_time = mid - 1
        total = m
        for t in time:
            total += prev_time // t
        return total
    
    left = min(time)
    right = max(time) * n
    ans = right
    mid = right
    
    if n <=m:
        return n
    
    while left<=right:
        total = m
        mid = (left+right) // 2

        for t in time:
            total += mid//t
            if total >= n:
                right = mid - 1
                ans = mid
                break
        else:
            if total < n:
                left = mid +1
            
    prev_total= get_prev_time_list(time, ans)
    # print(prev_total)
    answer = 0
    for idx in range(m):
        if ans % time[idx] == 0:
            prev_total += 1
            if prev_total == n:
                answer = idx+1
                break 
    return answer

n,m = map(int, sys.stdin.readline().split())
time = [*map(int, sys.stdin.readline().split())]

print(solution(n,m,time))

 

Comments