- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
你能帮我算法吗:
给定 2 个相同大小的数组 a[]
和 b[]
具有大于或等于 1 的整数。
查找不相等的索引 i
和 j
( i != j
) 使得值 -max(a[i]*b[i] + a[i] * b[j] + a[j] * b[j], a[i]*b[i] + a[j] * b[i] + a[j] * b[j])
将是最大的。
例子:a = [1, 9, 6, 6]
和 b = [9, 1, 6, 6].
最大值将在 i = 2 和 j = 3(从零开始的索引):a[2]*b[2] + a[2]*b[3] + a[3] * b[3] = 6*6+6*6+6*6 = 108
有没有办法在小于二次方的时间内找到 i 和 j?
以及在小于二次时间内找到目标函数值的相同问题?
谢谢!
最佳答案
这是我尝试实现 David Eisenstat's idea .我认为i != j
限制使这变得更加复杂,但无论哪种方式,我都欢迎有关改进代码的建议。最后有一个对抗蛮力的测试。
线路上部信封的构造A[i]*x + A[i]*B[i]
依赖于应用于从线转换而来的对偶点的 Andrew 单调链凸包算法。
python 代码:
# Upper envelope of lines in the plane
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])
# lower_hull has the structure [(dual_point, original_line, leftmost_x, index)]
def get_segment_idx(lower_hull, x):
lo = 0
hi = len(lower_hull) - 1
# Find the index of the first
# x coordinate greater than x
while lo < hi:
mid = lo + (hi - lo + 1) // 2
if lower_hull[mid][2] <= x:
lo = mid
else:
hi = mid - 1
return lo
# Assumes we add points in order of increasing x-coordinates
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)
# Each segment of the lower hull
# in the dual plane is a line intersection
# in the primal plane.
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
# Assumes we add points in order of decreasing x-coordinates
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)
# Each segment of the lower hull
# in the dual plane is a line intersection
# in the primal plane.
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
# Maximise A[i] * B[i] + A[i] * B[j] + A[j] * B[j]
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))]
# Convert to points in the dual plane
# [dual_point, original_line, leftmost x coordinate added later, original index]
dual_points = [[get_dual_point(line), line, None, i] for line, i in indexed_lines]
# Sort points by x coordinate ascending
sorted_points = sorted(dual_points, key=lambda x: x[0][0])
if debug:
print("sorted points")
print(sorted_points)
# Build lower hull, left to right
lower_hull = []
add_right_to_lower_hull(lower_hull, sorted_points[0])
for i in range (1, len(sorted_points)):
# Query the point before inserting it
# because of the stipulation that i != j
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)
# Build lower hull, right to left
lower_hull = collections.deque()
lower_hull.append(sorted_points[len(sorted_points) - 1])
for i in range (len(sorted_points) - 2, -1, -1):
# Query the point before inserting it
# because of the stipulation that i != j
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
#A = [1, 9, 6, 6]
#B = [9, 1, 6, 6]
#print("")
#print(f(A, B))
# Test
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.")
关于arrays - 最大化 A[i]*B[i] + A[i]*B[j] + A[j]*B[j], i != j,给定两个正整数的无序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69044040/
在C语言中,当有变量(假设都是int)i小于j时,我们可以用等式 i^=j^=i^=j 交换两个变量的值。例如,令int i = 3,j = 5;在计算 i^=j^=i^=j 之后,我有 i = 5,
我为以下问题编写了以下代码: 给定一个由 N 个正整数组成的序列 A,编写一个程序来查找满足 i > A[j]A[i](A[i] 的 A[j] 次方 > A[j] 的 A[i] 次方)。 我的代码通过
这个表达式是从左到右解析的吗?我试图解释解析的结果,但最后的结果是错误的。 int j=10, k=10; j+=j-=j*=j; //j=j+(j-=j*=j)=j+(j-j*j) k+=k*=
给定一个整数数组 A ,我试图找出在给定位置 j ,A[j] 从每个 i=0 到 i=j 在 A 中出现了多少次。我设计了一个如下所示的解决方案 map CF[400005]; for(int i=0
你能帮我算法吗: 给定 2 个相同大小的数组 a[]和 b[]具有大于或等于 1 的整数。 查找不相等的索引 i和 j ( i != j ) 使得值 -max(a[i]*b[i] + a[i] * b
每次用J的M.副词,性能显着下降。因为我怀疑艾弗森和许比我聪明得多,我一定是做错了什么。 考虑 Collatz conjecture .这里似乎有各种各样的内存机会,但不管我放在哪里M. ,性能太差了
假设一个包含各种类型的盒装矩阵: matrix =: ('abc';'defgh';23),:('foo';'bar';45) matrix +---+-----+--+|abc|defgh|23|+
是否有可能对于两个正整数 i 和 j,(-i)/j 不等于 -(i/j)?我不知道这是否可能......我认为这将是关于位的东西,或者 char 类型的溢出或其他东西,但我找不到它。有什么想法吗? 最
假设两个不同大小的数组: N0 =: i. 50 N1 =: i. 500 应该有一种方法可以获得唯一的对,只需将两者结合起来即可。我发现的“最简单”是: ]$R =: |:,"2 |: (,.N0)
我是 J 的新用户,我只是想知道 J 包中是否实现了三次样条插值方法? 最佳答案 我自己不熟悉,但是我确实安装了所有的包,所以 $ rg -l -i spline /usr/share/j/9.02
在 Q/kdb 中,您可以使用 ': 轻松修改动词,它代表每个优先级。它会将动词应用于一个元素及其之前的邻居。例如 =': 检查值对是否相等。在 J 中,您可以轻松折叠 /\ 但它是累积的,是否有成对
嗨,我有一个 4x4 双矩阵 A 1+2i 2-1i -3-2i -1+4i 3-1i -3+2i 1-3i -1-3i 4+3i 3+5i 1-2i -1-4i
刚刚发现 J 语言,我输入: 1+^o.*0j1 I expected the answer to be 0 ,但我得到了 0j1.22465e_16。虽然这非常接近于 0,但我想知道为什么 J 应该
这个问题在这里已经有了答案: With arrays, why is it the case that a[5] == 5[a]? (20 个答案) 关闭 3 年前。 我正在阅读“C++ 编程语言”
当第一行是 1, 1/2 , 1/3 ....这是支持该问题的图像。 是否存在比朴素的 O(n^2) 方法更有效的方法? 我在研究伯努利数时遇到了这个问题,然后在研究“Akiyama-Tanigawa
我写了一段Java代码,它在无限循环中运行。 下面是代码: public class TestProgram { public static void main(String[] args){
for (int i = n; i > 0; i /= 2) { for (int j = 0; j 0; i /= 2) 的第一个循环结果 O(log N) . 第二个循环for (int
如问题中所述,需要找到数组中 (i,j) 对的总数,使得 (1) **ia[j]** 其中 i 和 j 是数组的索引。没有空间限制。 我的问题是 1) Is there any approach w
for l in range(1,len(S)-1): for i in range(1,len(S)-l): j=i+l for X in N:
第二个for循环的复杂度是多少?会是n-i吗?根据我的理解,第一个 for 循环将执行 n 次,但第二个 for 循环中的索引设置为 i。 //where n is the number elemen
我是一名优秀的程序员,十分优秀!