Tenka1 Programmer Contest - CARtesian Coodinate

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}.

Examples

Input

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


Output

1.000000000000000 1.000000000000000


Input

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


Output

-1.000000000000000 -1.000000000000000


Input

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


Output

-1.722222222222222 1.325000000000000

Source: atcoder | Difficulty: UNKNOWN_DIFFICULTY


Solutions:
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 get_rank(L):
	D = {x: i for (i, x) in enumerate(sorted(L))}
	return [D[x] for x in L]

def calc(Z):
	Z.sort()
	(l, r) = (-1 << 28, 1 << 28)
	while (r - l) / max(abs(l), 1) > 1e-09:
		m = (l + r) / 2
		A = get_rank([s * m + t for (s, t) in Z])
		c = bit(A)
		if c > N * (N - 1) // 4:
			l = m
		else:
			r = m
	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))


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

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

def comp(L):
	S = list(set(L))
	S.sort()
	return {i: e for (e, i) in enumerate(S)}
import sys, random
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())
line = [(A[i] / B[i], i) for i in range(N)]
line.sort()
line = [line[i][1] for i in range(N)]
line2 = [(B[i] / A[i], i) for i in range(N)]
line2.sort()
line2 = [line2[i][1] for i in range(N)]

def cond(n):
	tempC = [C[i] - n * A[i] for i in range(N)]
	data = [tempC[i] / B[i] for i in range(N)]
	data = comp(data)
	bit = BIT(len(data))
	res = 0
	for i in range(N):
		id2 = data[tempC[line[i]] / B[line[i]]]
		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)]
	data = [tempC[i] / A[i] for i in range(N)]
	data = comp(data)
	bit = BIT(len(data))
	res = 0
	for i in range(N):
		id2 = data[tempC[line2[i]] / A[line2[i]]]
		res += i - bit.query(id2)
		bit.update(id2 + 1, 1)
	num = N * (N - 1) // 2
	return res >= (num + 1) // 2
start = -2 * 10 ** 8
end = 2 * 10 ** 8
count = 0
while end - start > 10 ** (-9):
	count += 1
	test = (end + start) / 2
	if cond(test):
		end = test
	else:
		start = test
	if (end - start) * 10 ** 9 < min(abs(start), abs(end)):
		break
start2 = -2 * 10 ** 8
end2 = 2 * 10 ** 8
while end2 - start2 > 10 ** (-9):
	test = (end2 + start2) / 2
	count += 1
	if cond2(test):
		end2 = test
	else:
		start2 = test
	if (end2 - start2) * 10 ** 9 < min(abs(start2), abs(end2)):
		break
print(end, end2)


import sys
readline = sys.stdin.readline

class BIT:

	def __init__(self, n):
		self.size = n
		self.tree = [0] * (n + 1)
		self.p = 2 ** (n.bit_length() - 1)
		self.dep = n.bit_length()

	def get(self, i):
		s = 0
		while i > 0:
			s += self.tree[i]
			i -= i & -i
		return s

	def add(self, i, x):
		while i <= self.size:
			self.tree[i] += x
			i += i & -i

	def bl(self, v):
		if v <= 0:
			return -1
		s = 0
		k = self.p
		for _ in range(self.dep):
			if s + k <= self.size and self.tree[s + k] < v:
				s += k
				v -= self.tree[s + k]
			k //= 2
		return s + 1

	def reset(self):
		self.tree = [0] * (self.size + 1)
N = int(readline())
INF = 200000000.0 + 10
EPS = 1e-09
Points = [tuple(map(int, readline().split())) for _ in range(N)]
Points.sort(key=lambda x: -x[0] / x[1])
XA = [-a / b for (a, b, _) in Points]
XB = [c / b for (_, b, c) in Points]
Points.sort(key=lambda x: -x[1] / x[0])
YA = [-b / a for (a, b, _) in Points]
YB = [c / a for (a, _, c) in Points]
con = -(-N * (N - 1) // 4)
T = BIT(N)
ng = -INF
ok = INF
nf = con
of = con
while abs(ok - ng) / max(1, min(abs(ok), abs(ng))) > EPS:
	med = (ok * nf + ng * of) / (of + nf)
	T.reset()
	res = 0
	for idx in sorted(range(N), key=lambda x: XA[x] * med + XB[x]):
		res += T.get(idx + 1)
		T.add(idx + 1, 1)
	if res >= con:
		ok = med
		of = ((res - con) * 2 + 1) ** 0.3
	else:
		ng = med
		nf = ((con - res) * 2) ** 0.3
X = ok
ng = -INF
ok = INF
nf = con
of = con
cnt = 1
while abs(ok - ng) / max(1, min(abs(ok), abs(ng))) > EPS:
	cnt += 1
	med = (ok * nf + ng * of) / (of + nf)
	T.reset()
	res = 0
	for idx in sorted(range(N), key=lambda x: YA[x] * med + YB[x]):
		res += T.get(idx + 1)
		T.add(idx + 1, 1)
	if res >= con:
		ok = med
		of = ((res - con) * 2 + 1) ** 0.3
	else:
		ng = med
		nf = ((con - res) * 2) ** 0.3
Y = ok
print(X, Y)


import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
ABC = list(map(int, read().split()))
A = ABC[::3]
B = ABC[1::3]
C = ABC[2::3]

class BIT:

	def __init__(self, max_n):
		self.size = max_n + 1
		self.tree = [0] * self.size

	def get_sum(self, i):
		s = 0
		while i:
			s += self.tree[i]
			i -= i & -i
		return s

	def add(self, i, x):
		while i < self.size:
			self.tree[i] += x
			i += i & -i

def Inversion(seq):
	N = len(seq)
	bit = BIT(N)
	inv = N * (N - 1) // 2
	for x in seq:
		inv -= bit.get_sum(x)
		bit.add(x, 1)
	return inv

def get_rank(seq):
	x_to_i = {x: i for (i, x) in enumerate(sorted(seq))}
	return [1 + x_to_i[x] for x in seq]

def solve_x(A, B, C):
	M = (N * (N - 1) // 2 + 1) // 2
	U = 2 * 10 ** 8 + 100
	left = -U
	right = U
	order = sorted(range(N), key=lambda i: A[i] / B[i])

	def test(x):
		y = [(C[i] - A[i] * x) / B[i] for i in order]
		seq = get_rank(y)
		return Inversion(seq) >= M
	for _ in range(60):
		mid = (left + right) / 2
		if test(mid):
			right = mid
		else:
			left = mid
	return (left + right) / 2
x = solve_x(A, B, C)
y = solve_x(B, A, C)
print(x, y)


class Bit:

	def __init__(self, a):
		if hasattr(a, '__iter__'):
			le = len(a)
			self.n = 1 << le.bit_length()
			self.values = values = [0] * (self.n + 1)
			values[1:le + 1] = a[:]
			for i in range(1, self.n):
				values[i + (i & -i)] += values[i]
		elif isinstance(a, int):
			self.n = 1 << a.bit_length()
			self.values = [0] * (self.n + 1)
		else:
			raise TypeError

	def add(self, i, val):
		(n, values) = (self.n, self.values)
		while i <= n:
			values[i] += val
			i += i & -i

	def sum(self, i):
		values = self.values
		res = 0
		while i > 0:
			res += values[i]
			i -= i & -i
		return res

	def bisect_left(self, v):
		(n, values) = (self.n, self.values)
		if v > values[n]:
			return None
		(i, step) = (0, n >> 1)
		while step:
			if values[i + step] < v:
				i += step
				v -= values[i]
			step >>= 1
		return i + 1

def inversion_number(arr):
	n = len(arr)
	arr = sorted(range(n), key=lambda x: arr[x])
	bit = Bit(n)
	res = n * (n - 1) >> 1
	for val in arr:
		res -= bit.sum(val + 1)
		bit.add(val + 1, 1)
	return res
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):
	if N < 100:
		ok = -10000000000.0
		ng = 10000000000.0
		n_iteration = 70
	else:
		ok = -10000.0
		ng = 10000.0
		n_iteration = 46
	(A, B, C) = zip(*sorted(zip(A, B, C), key=lambda x: -x[0] / x[1]))
	for _ in range(n_iteration):
		x = (ok + ng) * 0.5
		Y = [(-a * x + c) / b for (a, b, c) in zip(A, B, C)]
		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))


from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce, lru_cache
from fractions import gcd
import sys

def input():
	return sys.stdin.readline().rstrip()

def I():
	return int(input())

def Is():
	return (int(x) for x in input().split())

def LI():
	return list(Is())

def TI():
	return tuple(Is())

def IR(n):
	return [I() for _ in range(n)]

def LIR(n):
	return [LI() for _ in range(n)]

def TIR(n):
	return [TI() for _ in range(n)]

def S():
	return input()

def Ss():
	return input().split()

def LS():
	return list(S())

def SR(n):
	return [S() for _ in range(n)]

def SsR(n):
	return [Ss() for _ in range(n)]

def LSR(n):
	return [LS() for _ in range(n)]
sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 9 + 7
INF = 10 ** 18
EPS = 1e-09

class BinaryIndexedTree:

	def __init__(self, n):
		self.size = n
		self.tree = [0] * (n + 1)

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

	def add(self, i, x):
		while i <= self.size:
			self.tree[i] += x
			i += i & -i

def compress(A):
	dic = {element: i for (i, element) in enumerate(sorted(set(A)), start=1)}
	return [dic[a] for a in A]

def meguru_bisect(ng, ok, check):
	for _ in range(80):
		if abs(ok - ng) < EPS:
			break
		mid = (ok + ng) / 2
		if check(mid):
			ok = mid
		else:
			ng = mid
	return ok

def check_x(num):
	bit = BinaryIndexedTree(n)
	inversion_num = 0
	X = compress([(c - b * num) / a for (a, b, c) in X_ABC])
	for (i, x) in enumerate(X, start=1):
		bit.add(x, 1)
		inversion_num += i - bit.sum(x)
	return inversion_num >= m

def check_y(num):
	bit = BinaryIndexedTree(n)
	inversion_num = 0
	Y = compress([(c - a * num) / b for (a, b, c) in Y_ABC])
	for (i, y) in enumerate(Y, start=1):
		bit.add(y, 1)
		inversion_num += i - bit.sum(y)
	return inversion_num >= m
n = I()
ABC = TIR(n)
m = (n * (n - 1) // 2 + 1) // 2
X_ABC = sorted(ABC, key=lambda abc: abc[1] / abc[0])
Y_ABC = sorted(ABC, key=lambda abc: abc[0] / abc[1])
mid_x = meguru_bisect(-10 ** 9, 10 ** 9, check_x)
mid_y = meguru_bisect(-10 ** 9, 10 ** 9, check_y)
print(mid_y, mid_x)