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