File size: 1,083 Bytes
1ca0407
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""21 disjoint circles in a rectangle of perimeter 4, maximize the sum of radii
(EinsteinArena circles-rectangle, MIT).

Upstream bug fixed: the perimeter test is exact (Fractions) upstream but the disjointness test compares
float sqrt distances; both are exact here."""
from fractions import Fraction

from run import Invalid, float_matrix


def check(inst, ans):
    n = inst["n"]
    c = float_matrix(ans.get("circles"), n, 3, "circles")
    F = [tuple(Fraction(v) for v in row) for row in c]
    if any(r <= 0 for _, _, r in F):
        raise Invalid("radii must be positive")
    w = max(x + r for x, _, r in F) - min(x - r for x, _, r in F)
    h = max(y + r for _, y, r in F) - min(y - r for _, y, r in F)
    if w + h > 2:
        raise Invalid("bounding rectangle violates width + height <= 2")
    for i in range(n):
        xi, yi, ri = F[i]
        for j in range(i + 1, n):
            xj, yj, rj = F[j]
            if (xi - xj) ** 2 + (yi - yj) ** 2 < (ri + rj) ** 2:
                raise Invalid(f"circles {i} and {j} overlap")
    return sum(r for _, _, r in c), {}