这是我尝试实现
David Eisenstat的想法。我认为
i != j
的限制使得这更加复杂,但无论如何,我欢迎改进代码的建议。在最后进行了一项针对暴力方法的测试。
对于线条
A[i]*x + A[i]*B[i]
的上凸壳构建依赖于将从线条转换而来的对偶点应用于Andrew的单调链凸包算法。
Python代码:
from fractions import Fraction
import collections
def get_dual_point(line):
return (line[0], -line[1])
def get_primal_point(dual_line):
return (dual_line[0], -dual_line[1])
def get_line_from_two_points(p1, p2):
if p1[0] == p2[0]:
return (float('inf'), 0)
m = Fraction(p1[1] - p2[1], p1[0] - p2[0])
b = -m * p1[0] + p1[1]
return (m, b)
def cross_product(o, a, b):
return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])
def get_segment_idx(lower_hull, x):
lo = 0
hi = len(lower_hull) - 1
while lo < hi:
mid = lo + (hi - lo + 1) // 2
if lower_hull[mid][2] <= x:
lo = mid
else:
hi = mid - 1
return lo
def add_right_to_lower_hull(lower_hull, point):
while len(lower_hull) > 0 and lower_hull[-1][0][0] == point[0][0] and lower_hull[-1][0][1] > point[0][1]:
lower_hull.pop()
while len(lower_hull) > 1 and cross_product(lower_hull[-2][0], lower_hull[-1][0], point[0]) <= 0:
lower_hull.pop()
if not lower_hull or lower_hull[-1][0][0] != point[0][0]:
lower_hull.append(point)
if len(lower_hull) == 1:
lower_hull[0][2] = -float('inf')
else:
line = get_line_from_two_points(lower_hull[-1][0], lower_hull[-2][0])
lower_hull[-1][2] = get_primal_point(line)[0]
return lower_hull
def add_left_to_lower_hull(lower_hull, point):
while len(lower_hull) > 0 and lower_hull[0][0][0] == point[0][0] and lower_hull[0][0][1] > point[0][1]:
lower_hull.popleft()
while len(lower_hull) > 1 and cross_product(lower_hull[1][0], lower_hull[0][0], point[0]) >= 0:
lower_hull.popleft()
if not lower_hull or lower_hull[0][0][0] != point[0][0]:
lower_hull.appendleft(point)
if len(lower_hull) == 1:
lower_hull[0][2] = -float('inf')
else:
line = get_line_from_two_points(lower_hull[0][0], lower_hull[1][0])
lower_hull[1][2] = get_primal_point(line)[0]
return lower_hull
def f(A, B):
debug = False
if debug:
print("A: %s" % A)
print("B: %s" % B)
best = -float('inf')
best_idxs = ()
indexed_lines = [((A[i], A[i] * B[i]), i) for i in range(len(A))]
dual_points = [[get_dual_point(line), line, None, i] for line, i in indexed_lines]
sorted_points = sorted(dual_points, key=lambda x: x[0][0])
if debug:
print("sorted points")
print(sorted_points)
lower_hull = []
add_right_to_lower_hull(lower_hull, sorted_points[0])
for i in range (1, len(sorted_points)):
idx = sorted_points[i][3]
segment_idx = get_segment_idx(lower_hull, B[idx])
m, b = lower_hull[segment_idx][1]
j = lower_hull[segment_idx][3]
candidate = m * B[idx] + b + A[idx] * B[idx]
if debug:
print("segment: %s, idx: %s, B[idx]: %s" % (segment_idx, idx, B[idx]))
if candidate > best:
best = candidate
best_idxs = (idx, j)
add_right_to_lower_hull(lower_hull, sorted_points[i])
if debug:
print("lower hull")
print(lower_hull)
lower_hull = collections.deque()
lower_hull.append(sorted_points[len(sorted_points) - 1])
for i in range (len(sorted_points) - 2, -1, -1):
idx = sorted_points[i][3]
segment_idx = get_segment_idx(lower_hull, B[idx])
m, b = lower_hull[segment_idx][1]
j = lower_hull[segment_idx][3]
candidate = m * B[idx] + b + A[idx] * B[idx]
if debug:
print("segment: %s, idx: %s, B[idx]: %s" % (segment_idx, idx, B[idx]))
if candidate > best:
best = candidate
best_idxs = (idx, j)
add_left_to_lower_hull(lower_hull, sorted_points[i])
if debug:
print("lower hull")
print(lower_hull)
return best, best_idxs
import random
def brute_force(A, B):
best = -float('inf')
best_idxs = ()
for i in range(len(A)):
for j in range(len(B)):
if i != j:
candidate = A[i] * B[i] + A[i] * B[j] + A[j] * B[j]
if candidate > best:
best = candidate
best_idxs = (i, j)
return best, best_idxs
num_tests = 500
n = 20
m = 1000
for _ in range(num_tests):
A = [random.randint(1, m) for i in range(n)]
B = [random.randint(1, m) for i in range(n)]
_f = f(A, B)
_brute = brute_force(A, B)
if _f[0] != _brute[0]:
print("Mismatch")
print(A)
print(B)
print(_f, _brute)
print("Done testing.")