Algorithm 最小化和的元素?

Algorithm 最小化和的元素?,algorithm,dynamic-programming,Algorithm,Dynamic Programming,我发现一个问题表明: Let's consider a vector x = (x1, x2 ... xn) with real elements. 1). Sort the vector -- easy 2). Find a real number a so that sum ( abs (xi - a) ) is minim 我不知道数组排序是否有帮助,但对于2)。我想我可以对向量中的所有元素进行算术求和,并说平均值就是我们要找的a 但这是不对的。例如: x = (1, 10, 10)

我发现一个问题表明:

Let's consider a vector x = (x1, x2 ... xn) with real elements. 
1). Sort the vector  -- easy
2). Find a real number a so that sum ( abs (xi - a) ) is minim
我不知道数组排序是否有帮助,但对于
2)。
我想我可以对向量中的所有元素进行算术求和,并说平均值就是我们要找的a

但这是不对的。例如:

x = (1, 10, 10)
avg = [ 21/3 ] = 7 = a
sum = |1 - 7| + |10 - 7| + |10 - 7| = 6 + 3 + 3 = 12
<如果我们考虑a=10,我们得到

sum = |1 - 10| + |10 - 10| + |10 - 10| = 9 < 12
sum=| 1-10 |+| 10-10 |+| 10-10 |=9<12
我能想到的另一个解决方案是从最小元素到最高元素的蛮力,步长为
I+=0.1

您要寻找的是-而不是平均值

这是因为你所寻找的实际上是-如果你只有一个维度,这恰好是标准的中位数(你有)

中值可以在
O(n)
using中找到,因此排序是多余的