목차

Load Balancing (Silver)

ps
링크acmicpc.net/…
출처BOJ
문제 번호11997
문제명Load Balancing (Silver)
레벨골드 4
분류

누적합

시간복잡도O(n^2)
인풋사이즈n<=1000
사용한 언어Python
제출기록55836KB / 688ms
최고기록916ms
해결날짜2022/05/30
태그

[라이] 구간합 배열

풀이

코드

"""Solution code for "BOJ 11997. Load Balancing (Silver)".

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

import sys

INF = float('inf')


def create_2d_prefix_sum(nums):
    prefix_sum = [[0] * (len(nums[0]) + 1)]
    for row in nums:
        prefix_sum.append([ps := 0] +
                          [(ps := ps + num) + p
                           for num, p in zip(row, prefix_sum[-1][1:])])
    return prefix_sum


def main():
    N = int(sys.stdin.readline())
    x_coords, y_coords = [], []
    for _ in range(N):
        x, y = [int(x) for x in sys.stdin.readline().split()]
        x_coords.append(x)
        y_coords.append(y)

    x_comp_map = {x: i for i, x in enumerate(sorted(set(x_coords)))}
    y_comp_map = {y: i for i, y in enumerate(sorted(set(y_coords)))}
    nums = [[0] * len(x_comp_map) for _ in y_comp_map]
    for x_i, y_i in zip(x_coords, y_coords):
        nums[y_comp_map[y_i]][x_comp_map[x_i]] = 1

    prefix_sum = create_2d_prefix_sum(nums)
    bottom_row = prefix_sum[-1]
    total = bottom_row[-1]
    answer = INF
    for row in prefix_sum:
        ps_r = row[-1]
        for ps_rc, ps_c in zip(row, bottom_row):
            up_left = ps_rc
            up_right = ps_r - up_left
            down_left = ps_c - up_left
            down_right = total - up_right - down_left - up_left
            answer = min(answer, max(up_left, up_right, down_left, down_right))
            
    print(answer)


if __name__ == '__main__':
    main()