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()