結果
| 問題 |
No.1874 Minimum of Sum of Rectangles
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-26 15:56:58 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 856 ms / 3,000 ms |
| コード長 | 1,430 bytes |
| コンパイル時間 | 199 ms |
| コンパイル使用メモリ | 82,796 KB |
| 実行使用メモリ | 111,476 KB |
| 最終ジャッジ日時 | 2025-03-26 15:57:34 |
| 合計ジャッジ時間 | 20,768 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 39 |
ソースコード
import sys
def readints():
return list(map(int, sys.stdin.readline().split()))
def weighted_median(values_weights):
sorted_vw = sorted(values_weights, key=lambda x: x[0])
total = sum(w for v, w in sorted_vw)
half = total / 2
current = 0
for v, w in sorted_vw:
current += w
if current >= half:
return v
return sorted_vw[-1][0] if sorted_vw else 0
def main():
n = int(sys.stdin.readline())
points = [tuple(map(int, sys.stdin.readline().split())) for _ in range(n)]
xs = [x for x, y in points]
ys = [y for x, y in points]
# Initial x and y as medians
sorted_xs = sorted(xs)
x = sorted_xs[n // 2]
sorted_ys = sorted(ys)
y = sorted_ys[n // 2]
prev_x, prev_y = None, None
max_iter = 20 # Prevent infinite loop
iter_count = 0
while (prev_x != x or prev_y != y) and iter_count < max_iter:
iter_count += 1
prev_x, prev_y = x, y
# Compute y with weights |x - xi|
weights_y = [(yi, abs(x - xi)) for xi, yi in points]
y = weighted_median(weights_y)
# Compute x with weights |y - yi|
weights_x = [(xi, abs(y - yi)) for xi, yi in points]
x = weighted_median(weights_x)
# Calculate the sum
total = 0
for xi, yi in points:
total += abs(x - xi) * abs(y - yi)
print(total)
if __name__ == "__main__":
main()
lam6er