Problem:


Score : 800 points

Problem Statement

There are N lines in the xy-plane. The i-th line is represented by A_ix+B_iy=C_i.
Any two lines among the N+2 lines, the above N lines plus the x-axis and y-axis, cross each other at exactly one point.

For each pair 1 \leq i < j \leq N, there is a car at the cross point of the i-th and j-th lines.
Even where three or more lines intersect at a point, a car is individually placed for each pair of lines.
That is, there will be k(k-1)/2 cars placed at the intersection of k lines.

Those cars are already very old, and can only be moved parallel to the x-axis or y-axis.

Takahashi will hold an exhibition of antique cars at a place on the xy-plane.
In order to avoid damaging the half-broken cars too much, he will select the place of the exhibition so that the total distance covered will be minimized when all the cars are moved to the place.
If such a place is not uniquely determined, among the places that satisfy the condition above, the place with the minimum x-coordinate will be selected.
If the place is still not uniquely determined, among the places that satisfy the two conditions above, the place with the minimum y-coordinate will be selected.

Find the place of the exhibition that will be selected.

Constraints

2 \leq N \leq 4 × 10^4

1 \leq |A_i|,|B_i| \leq 10^4(1 \leq i \leq N)

0 \leq |C_i| \leq 10^4(1 \leq i \leq N)

No two given lines are parallel.

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N
A_1 B_1 C_1
:
A_N B_N C_N

Outputs

Print the x-coordinate and y-coordinate of the place of the exhibition that will be selected, in this order, with a space in between. The output will be judged as correct when the absolute or relative error is at most 10^{-9}.

Sample Input 1

3
1 1 1
2 -1 2
-1 2 2

Sample Output 1

1.000000000000000 1.000000000000000

There is a car at each place shown by a blue circle in the figure. The place to be selected is shown by a purple circle.

Sample Input 2

4
1 1 2
1 -1 0
3 -1 -2
1 -3 4

Sample Output 2

-1.000000000000000 -1.000000000000000

Sample Input 3

7
1 7 8
-2 4 9
3 -8 -5
9 2 -14
6 7 5
-8 -9 3
3 8 10

Sample Output 3

-1.722222222222222 1.325000000000000



Sample input:
3
1 1 1
2 -1 2
-1 2 2


Sample output:
1.000000000000000 1.000000000000000

Submission status: Runtime Error

Solution code:
import os
import sys
import numpy as np

# >>> binary indexed tree >>>
# 必要な要素数+1 の長さの ndarray の 1 要素目以降を使う

def bit_sum(bit, i):  # ["bit_sum", "i8(i8[:],i8)"],
    # (0, i]
    res = 0
    while i:
        res += bit[i]
        i -= i & -i
    return res

def bit_add(bit, i, val):  # ["bit_add", "void(i8[:],i8,i8)"],
    n = len(bit)
    while i < n:
        bit[i] += val
        i += i & -i

# <<< binary indexed tree <<<


def inversion_number(arr):  # ["inversion_number", "i8(f8[:])"],
    # 転倒数
    n = len(arr)
    arr = np.argsort(arr) + 1
    bit = np.zeros(n+1, dtype=np.int64)
    res = n * (n-1) >> 1
    for val in arr:
        res -= bit_sum(bit, val)
        bit_add(bit, val, 1)
    return res


# >>> numba compile >>

numba_config = [
    ["bit_sum", "i8(i8[:],i8)"],
    ["bit_add", "void(i8[:],i8,i8)"],
    ["inversion_number", "i8(f8[:])"],
]
if sys.argv[-1] == "ONLINE_JUDGE":
    from numba.pycc import CC
    cc = CC("my_module")
    for func_name, types in numba_config:
        cc.export(func_name, types)(vars()[func_name])
        cc.compile()
    exit()
elif os.name == "posix":
    exec(f"from my_module import {','.join(func_name for func_name, _ in numba_config)}")
else:
    from numba import njit
    for func_name, types in numba_config:
        vars()[func_name] = njit(types, cache=True)(vars()[func_name])
    print("compiled", file=sys.stderr)

# <<< numba compile <<<


N = int(input())
ABC = [list(map(int, input().split())) for _ in range(N)]
A, B, C = zip(*ABC)
th = N*(N-1)//2 // 2 + 1
def solve(A, B, C):
    # y = (-Ax+C) / B
    if N < 100:
        ok = -1e10
        ng = 1e10
        n_iteration = 70
    else:
        ok = -1e4
        ng = 1e4
        n_iteration = 46
    A, B, C = zip(*sorted(zip(A, B, C), key=lambda x: -x[0]/x[1]))
    A = np.array(A)
    B = np.array(B)
    C = np.array(C)
    for _ in range(n_iteration):
        x = (ok+ng) * 0.5
        Y = (-A*x+C) / B
        inv_num = inversion_number(Y)
        if inv_num >= th:
            ok = x
        else:
            ng = x
    return ok

print(solve(A, B, C), solve(B, A, C))

Submission status: Runtime Error

Solution code:
import os
import sys

import numpy as np


def solve(inp):
    def intersection(a, b, c, d, e, f):
        x = (c * e - b * f) / (a * e - b * d)
        y = (f - d * x) / e
        return x, y

    n = int(inp[0])
    m = n * (n - 1) // 2
    aaa = inp[1::3]
    bbb = inp[2::3]
    ccc = inp[3::3]
    intersections_x = np.zeros(m, dtype=np.float64)
    intersections_y = np.zeros(m, dtype=np.float64)
    k = 0
    for i in range(n - 1):
        a = aaa[i]
        b = bbb[i]
        c = ccc[i]
        for j in range(i + 1, n):
            d = aaa[j]
            e = bbb[j]
            f = ccc[j]
            x, y = intersection(a, b, c, d, e, f)
            intersections_x[k] = x
            intersections_y[k] = y
            k += 1
    intersections_x.sort()
    intersections_y.sort()

    return intersections_x[(m - 1) // 2], intersections_y[(m - 1) // 2]


if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC

    cc = CC('my_module')
    cc.export('solve', '(f8[:],)')(solve)
    cc.compile()
    exit()

if os.name == 'posix':
    # noinspection PyUnresolvedReferences
    from my_module import solve
else:
    from numba import njit

    solve = njit('(f8[:],)', cache=True)(solve)

inp = np.fromstring(sys.stdin.buffer.read(), dtype=np.float64, sep=' ')
try:
    x, y = solve(inp)
    print(x, y)
except ZeroDivisionError:
    print('WA')
    exit()

Submission status: Runtime Error

Solution code:
import os
import sys

import numpy as np


def solve(inp):
    def intersection(a, b, c, d, e, f):
        x = (c * e - b * f) / (a * e - b * d)
        y = (f - d * x) / e
        return x, y

    n = int(inp[0])
    m = n * (n - 1) // 2
    aaa = inp[1::3]
    bbb = inp[2::3]
    ccc = inp[3::3]
    intersections_x = np.zeros(m, dtype=np.float64)
    intersections_y = np.zeros(m, dtype=np.float64)
    k = 0
    for i in range(n - 1):
        a = aaa[i]
        b = bbb[i]
        c = ccc[i]
        for j in range(i + 1, n):
            d = aaa[j]
            e = bbb[j]
            f = ccc[j]
            x, y = intersection(a, b, c, d, e, f)
            intersections_x[k] = x
            intersections_y[k] = y
            k += 1
    intersections_x.sort()
    intersections_y.sort()

    return intersections_x[(m - 1) // 2], intersections_y[(m - 1) // 2]


if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC

    cc = CC('my_module')
    cc.export('solve', '(f8[:],)')(solve)
    cc.compile()
    exit()

if os.name == 'posix':
    # noinspection PyUnresolvedReferences
    from my_module import solve
else:
    from numba import njit

    solve = njit('(f8[:],)', cache=True)(solve)

inp = np.fromstring(sys.stdin.buffer.read(), dtype=np.float64, sep=' ')
x, y = solve(inp)
print(x, y)

Submission status: Runtime Error

Solution code:
import os
import sys

import numpy as np


def solve(inp):
    def intersection(a, b, c, d, e, f):
        if a * e - b * d == 0:
            print('WA')
            return c, f
        x = (c * e - b * f) / (a * e - b * d)
        y = (f - d * x) / e
        return x, y

    n = int(inp[0])
    m = n * (n - 1) // 2
    aaa = inp[1::3]
    bbb = inp[2::3]
    ccc = inp[3::3]
    intersections_x = np.zeros(m, dtype=np.float64)
    intersections_y = np.zeros(m, dtype=np.float64)
    k = 0
    for i in range(n - 1):
        a = aaa[i]
        b = bbb[i]
        c = ccc[i]
        for j in range(i + 1, n):
            d = aaa[j]
            e = bbb[j]
            f = ccc[j]
            x, y = intersection(a, b, c, d, e, f)
            intersections_x[k] = x
            intersections_y[k] = y
            k += 1
    intersections_x.sort()
    intersections_y.sort()

    return intersections_x[(m - 1) // 2], intersections_y[(m - 1) // 2]


if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC

    cc = CC('my_module')
    cc.export('solve', '(f8[:],)')(solve)
    cc.compile()
    exit()

if os.name == 'posix':
    # noinspection PyUnresolvedReferences
    from my_module import solve
else:
    from numba import njit

    solve = njit('(f8[:],)', cache=True)(solve)

inp = np.fromstring(sys.stdin.read(), dtype=np.float64, sep=' ')
x, y = solve(inp)
print(x, y)

Submission status: Runtime Error

Solution code:
import os
import sys

import numpy as np


def solve(inp):
    def intersection(a, b, c, d, e, f):
        if a * e - b * d == 0:
            print('WA')
            exit()
        x = (c * e - b * f) / (a * e - b * d)
        y = (f - d * x) / e
        return x, y

    n = inp[0]
    m = n * (n - 1) // 2
    aaa = inp[1::3].astype(np.float64)
    bbb = inp[2::3].astype(np.float64)
    ccc = inp[3::3].astype(np.float64)
    intersections_x = np.zeros(m, dtype=np.float64)
    intersections_y = np.zeros(m, dtype=np.float64)
    k = 0
    for i in range(n - 1):
        a = aaa[i]
        b = bbb[i]
        c = ccc[i]
        for j in range(i + 1, n):
            d = aaa[j]
            e = bbb[j]
            f = ccc[j]
            x, y = intersection(a, b, c, d, e, f)
            intersections_x[k] = x
            intersections_y[k] = y
            k += 1
    intersections_x.sort()
    intersections_y.sort()

    return intersections_x[(m - 1) // 2], intersections_y[(m - 1) // 2]


if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC

    cc = CC('my_module')
    cc.export('solve', '(i8[:],)')(solve)
    cc.compile()
    exit()

if os.name == 'posix':
    # noinspection PyUnresolvedReferences
    from my_module import solve
else:
    from numba import njit

    solve = njit('(i8[:],)', cache=True)(solve)

inp = np.fromstring(sys.stdin.read(), dtype=np.int64, sep=' ')
x, y = solve(inp)
print(x, y)

Submission status: Runtime Error

Solution code:
import os
import sys

import numpy as np


def solve(inp):
    def intersection(a, b, c, d, e, f):
        if a == 0:
            y = c / b
            x = (f - e * y) / d
            return x, y
        if d == 0:
            y = f / e
            x = (c - b * y) / a
            return x, y
        if b == 0:
            x = c / a
            y = (f - d * x) / e
            return x, y
        if e == 0:
            x = f / d
            y = (c - a * x) / b
            return x, y
        x = (c * e - b * f) / (a * e - b * d)
        y = (f - d * x) / e
        return x, y

    n = inp[0]
    m = n * (n - 1) // 2
    aaa = inp[1::3].astype(np.float64)
    bbb = inp[2::3].astype(np.float64)
    ccc = inp[3::3].astype(np.float64)
    intersections_x = np.zeros(m, dtype=np.float64)
    intersections_y = np.zeros(m, dtype=np.float64)
    k = 0
    for i in range(n - 1):
        a = aaa[i]
        b = bbb[i]
        c = ccc[i]
        for j in range(i + 1, n):
            d = aaa[j]
            e = bbb[j]
            f = ccc[j]
            x, y = intersection(a, b, c, d, e, f)
            intersections_x[k] = x
            intersections_y[k] = y
            k += 1
    intersections_x.sort()
    intersections_y.sort()

    return intersections_x[(m - 1) // 2], intersections_y[(m - 1) // 2]


if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC

    cc = CC('my_module')
    cc.export('solve', '(i8[:],)')(solve)
    cc.compile()
    exit()

if os.name == 'posix':
    # noinspection PyUnresolvedReferences
    from my_module import solve
else:
    from numba import njit

    solve = njit('(i8[:],)', cache=True)(solve)

inp = np.fromstring(sys.stdin.read(), dtype=np.int64, sep=' ')
x, y = solve(inp)
print(x, y)

Submission status: Runtime Error

Solution code:
import os
import sys

import numpy as np


def solve(inp):
    def intersection(a, b, c, d, e, f):
        if a == 0:
            y = c / b
            x = (f - e * y) / d
            return x, y
        if d == 0:
            y = f / e
            x = (c - b * y) / a
            return x, y
        if b == 0:
            x = c / a
            y = (f - d * x) / e
            return x, y
        if e == 0:
            x = f / d
            y = (c - a * x) / b
            return x, y
        x = (c * e - b * f) / (a * e - b * d)
        y = (f - d * x) / e
        return x, y

    n = inp[0]
    m = n * (n - 1) // 2
    aaa = inp[1::3]
    bbb = inp[2::3]
    ccc = inp[3::3]
    intersections_x = np.zeros(m, dtype=np.float64)
    intersections_y = np.zeros(m, dtype=np.float64)
    k = 0
    for i in range(n - 1):
        a = aaa[i]
        b = bbb[i]
        c = ccc[i]
        for j in range(i + 1, n):
            d = aaa[j]
            e = bbb[j]
            f = ccc[j]
            x, y = intersection(a, b, c, d, e, f)
            intersections_x[k] = x
            intersections_y[k] = y
            k += 1
    intersections_x.sort()
    intersections_y.sort()

    return intersections_x[m // 2], intersections_y[m // 2]


if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC

    cc = CC('my_module')
    cc.export('solve', '(i8[:],)')(solve)
    cc.compile()
    exit()

if os.name == 'posix':
    # noinspection PyUnresolvedReferences
    from my_module import solve
else:
    from numba import njit

    solve = njit('(i8[:],)', cache=True)(solve)

inp = np.fromstring(sys.stdin.read(), dtype=np.int64, sep=' ')
x, y = solve(inp)
print(x, y)

Submission status: Runtime Error

Solution code:
class BIT():
    def __init__(self,n):
        self.BIT=[0]*(n+1)
        self.num=n

    def query(self,idx):
        res_sum = 0
        while idx > 0:
            res_sum += self.BIT[idx]
            idx -= idx&(-idx)
        return res_sum

    #Ai += x O(logN)
    def update(self,idx,x):
        while idx <= self.num:
            self.BIT[idx] += x
            idx += idx&(-idx)
        return

def sort_func(ab0,ab1):
    a0, b0 = ab0
    a1, b1 = ab1
    return a1*(1+b0) - a0*(1+b1)

def comp(L):
    S=list(set(L))
    S.sort(key=cmp_to_key(sort_func))
    return {i:e for e,i in enumerate(S)}

import sys

input=sys.stdin.readline

N=int(input())
A=[0]*N;B=[0]*N;C=[0]*N
for i in range(N):
    A[i],B[i],C[i]=map(int,input().split())

def cond(n):
    tempC=[C[i]-n*A[i] for i in range(N)]
    line=[(A[i]/B[i],i) for i in range(N)]
    line.sort()
    data=[tempC[i]/B[i] for i in range(N)]
    data=comp(data)
    bit=BIT(len(data))
    res=0
    for i in range(N):
        val,id=line[i]
        id2=data[tempC[id]/B[id]]
        res+=i-bit.query(id2)
        bit.update(id2+1,1)
    num=N*(N-1)//2
    return res>=(num+1)//2

def cond2(n):
    tempC=[C[i]-n*B[i] for i in range(N)]
    line=[(B[i]/A[i],i) for i in range(N)]
    line.sort()
    data=[tempC[i]/A[i] for i in range(N)]
    data=comp(data)
    bit=BIT(len(data))
    res=0
    for i in range(N):
        val,id=line[i]
        id2=data[tempC[id]/A[id]]
        res+=i-bit.query(id2)
        bit.update(id2+1,1)
    num=N*(N-1)//2
    return res>=(num+1)//2

start=-10**5
end=10**5
while end-start>10**-5:
    test=(end+start)/2
    if cond(test):
        end=test
    else:
        start=test

start2=-10**5
end2=10**5
while end2-start2>10**-5:
    test=(end2+start2)/2
    if cond2(test):
        end2=test
    else:
        start2=test

print(end,end2)

Submission status: Runtime Error

Solution code:
def addbit(i):
    i += 1
    while i <= N:
        BIT[i] += 1
        i += i & (-i)
def getsum(i):
    ret = 0
    i += 1
    while i != 0:
        ret += BIT[i]
        i -= i&(-i)
    return ret
def bit(L):
    global BIT
    BIT=[0] * (N+1)
    re = 0
    for l in L:
        re += getsum(l)
        addbit(l)
    return N*(N-1)//2 - re
def calc(Z):
    Z.sort()
    l, r, lc, rc = -1<<32, 1<<32, N*(N-1)//4, N*(N-1)//4
    f = 1
    while r - l > 0.000000001:
        m = (l * rc + r * lc) / (lc + rc)
        A = [a&((1<<16)-1) for a in sorted([(int((s*m+t)*2**40)<<16)+i for i, (s, t) in enumerate(Z)])]
        c = bit(A)
        if c > N*(N-1)//4:
            l = m
            lc = c - N*(N-1)//4
        else:
            r = m
            rc = max(N*(N-1)//4 - c, 1)
        
        f ^= 1
    return l

N = int(input())
X, Y = [], []
for _ in range(N):
    a, b, c = map(int, input().split())
    X.append((-a/b, c/b))
    Y.append((-b/a, c/a))
print(calc(X), calc(Y))

Submission status: Runtime Error

Solution code:
# coding: utf-8

import math
import fractions
import heapq
import collections
import re
import array
import bisect
import itertools

from collections import Counter, defaultdict


def argsort(l, one_base=False):
    return sorted(range(one_base, len(l) + one_base), key=lambda i: l[i-one_base])


class BinaryIndexedTree(object):
    def __init__(self, N):
        self.N = N
        self.tree = [0] * (N + 1)

    def add(self, a, w):
        x = a
        while x <= self.N:
            self.tree[x] += w
            x += (x & -x)

    def sum(self, a):
        if a == 0:
            return 0
        s = 0
        x = a
        while x > 0:
            s += self.tree[x]
            x -= (x & -x)
        return s


def count_inv(A):
    bit = BinaryIndexedTree(100001)
    ans = 0
    for j in range(len(A)):
        ans += j - bit.sum(A[j])
        bit.add(A[j], 1)
    return ans


def count_crossed_lines(N, C, a, b, x):
    c = 2
    Y = [(-C[i][a] * x + C[i][c]) / C[i][b] for i in range(N)]
    yl = argsort(Y, one_base=True)
    c = count_inv(yl)
    return c


def find_coord(N, C, a, b):
    med_v = math.ceil(N * (N-1) / 4)
    iangles = [C[i][a] / C[i][b] for i in range(N)]
    coefs = [C[i] for i in argsort(iangles)]
    # search
    rg = [-100, 100]
    cl = [None, None]
    while True:
        if cl[0] is None:
            cl[0] = count_crossed_lines(N, coefs, a, b, rg[0])
        if cl[1] is None:
            cl[1] = count_crossed_lines(N, coefs, a, b, rg[1])
        if cl[0] < med_v:
            if med_v <= cl[1]:
                break
            rg = [rg[1], rg[1] * 2]
            cl = [cl[1], None]
        else:
            rg = [rg[0] * 2, rg[0]]
            cl = [None, rg[0]]
    # bin-search
    d = rg[1] - rg[0]
    m = (rg[1] + rg[0]) / 2
    while d > 1e-9:
        m = (rg[1] + rg[0]) / 2
        cl = count_crossed_lines(N, coefs, a, b, m)
        if cl < med_v:
            rg[0] = m
        else:
            rg[1] = m
        d = rg[1] - rg[0]
    return m


def main():
    N = int(input())
    coefs = [None] * N
    for i in range(N):
        a, b, c = map(int, input().split(" "))
        coefs[i] = (a, b, c)
    x = find_coord(N, coefs, 0, 1)
    y = find_coord(N, coefs, 1, 0)
    print("{} {}".format(x, y))

if __name__ == "__main__":
    main()