목차

과부화 방지

ps
링크acmicpc.net/…
출처BOJ
문제 번호31396
문제명과부화 방지
레벨플래티넘 5
분류

이분탐색

시간복잡도O((n+m)logm)
인풋사이즈n<200000, m<200000
사용한 언어Python 3.11
제출기록64308KB / 560ms
최고기록492ms
해결날짜2024/02/05

풀이

코드

"""Solution code for "BOJ 31396. 과부하 방지".

- Problem link: https://www.acmicpc.net/problem/31396
- Solution link: http://www.teferi.net/ps/problems/boj/31396

Tags: [greedy] [binary search]
"""

import functools
from teflib import binsearch


def is_valid(device_count, strip_psum, devices, init_socket_count):
    tot_strip_count = len(strip_psum) - 1
    used_strip_count = 0
    socket_count = init_socket_count
    max_strip_len = 0
    for d_i in devices[-device_count:]:
        while used_strip_count < tot_strip_count and max_strip_len < d_i:
            max_strip_len += 1
            newly_used_strip = min(
                socket_count, tot_strip_count - used_strip_count
            )
            socket_count += (
                strip_psum[used_strip_count + newly_used_strip]
                - strip_psum[used_strip_count]
                - newly_used_strip
            )
            used_strip_count += newly_used_strip
        if socket_count == 0:
            return False
        socket_count -= 1
    return True


def main():
    # pylint: disable=unused-variable
    N, K, M = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]
    D = [int(x) for x in input().split()]

    is_valid_func = functools.partial(
        is_valid,
        strip_psum=[v := 0] + [v := v + x for x in sorted(A, reverse=True)],
        devices=sorted(D),
        init_socket_count=K,
    )
    print(binsearch.maximum_valid_integer(1, M + 1, is_valid_func))


if __name__ == '__main__':
    main()