File size: 2,438 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""Edges vs triangles (Razborov) upper-bound curve (EinsteinArena edges-vs-triangles, MIT).

Each row is a probability vector over 20 parts (a complete 20-partite graphon); edge density
1 - sum w^2 ... (upstream uses S1^2 - S2 and S1^3 - 3 S1 S2 + 2 S3). Score = -(area under the slope-3
envelope + 10 * max gap) (maximize). Ported verbatim from the upstream verifier. Changes: at most
inst["max_rows"] rows (the description says m <= 500 but upstream did not enforce it); NaN/inf rejected."""
import numpy as np

from run import Invalid, float_matrix


def _densities(A):
    S1 = A.sum(1)
    S2 = (A ** 2).sum(1)
    S3 = (A ** 3).sum(1)
    return S1 ** 2 - S2, S1 ** 3 - 3 * S1 * S2 + 2 * S3


def _curve(ex, ty):
    slope = 3.0
    o = np.argsort(ex)
    fx = np.concatenate(([0.0], ex[o], [1.0]))
    fy = np.concatenate(([0.0], ty[o], [1.0]))
    ux, inv = np.unique(fx, return_inverse=True)
    my = np.full(len(ux), np.inf)
    np.minimum.at(my, inv, fy)
    fx, fy = ux, my
    if len(fx) < 2:
        return 5.0 / 6.0, 1.0
    left = np.minimum.accumulate(fy - slope * fx)
    right = np.minimum.accumulate(fy[::-1])[::-1]
    area = 0.0
    for i in range(len(fx) - 1):
        x0, x1 = fx[i], fx[i + 1]
        w = x1 - x0
        if w <= 0:
            continue
        cap = min(float(right[i + 1]), 1.0)
        yl, yr = slope * x0 + left[i], slope * x1 + left[i]
        if yl >= cap:
            area += cap * w
        elif yr <= cap:
            area += (yl + yr) * w / 2.0
        else:
            xh = (cap - left[i]) / slope
            area += (yl + cap) * (xh - x0) / 2.0 + cap * (x1 - xh)
    gaps = np.diff(fx)
    idx = np.where((fx[:-1] >= 0.0) & (fx[:-1] < 1.0))[0]
    return area, float(np.max(gaps[idx])) if idx.size else 0.0


def check(inst, ans):
    w = ans.get("weights")
    if not isinstance(w, list) or not 1 <= len(w) <= inst["max_rows"]:
        raise Invalid(f"weights must be a list of 1..{inst['max_rows']} rows")
    A = np.array(float_matrix(w, None, inst["parts"], "weights"))
    if np.any(A < 0):
        raise Invalid("weights must be non-negative")
    s = A.sum(1)
    if np.any(s < 1e-7):
        raise Invalid("a row sums to (nearly) zero")
    A = A / s[:, None]
    e, t = _densities(A)
    area, gap = _curve(e, t)
    val = -(area + 10 * gap)
    if not np.isfinite(val):
        raise Invalid("non-finite score")
    return float(val), {"area": float(area), "max_gap": gap}