最小化绝对值和

创建于:2024年12月31日

使用 O1 回答 Chat01

问题

设n属于N*,求|n-1949|+|n-1950|+...+|n-2023|的最小值

思考

Finding the median

OK, I’m thinking through minimizing the sum of absolute differences from 1949 to 2023. Since the data from 1949 to 2023 totals 75 points (an odd number), the median is the middle value.

Pinpointing the median

I’ve figured out that the median for these 75 numbers is 1986, both by locating the middle element and averaging the endpoints. It’s the optimal point to minimize the sum of absolute differences.

Assessing total distance

I'm pulling together the total distance from 1986 to each number from 1949 to 2023. Summing the distances from 1949 to 1986 and 1987 to 2023, I get a total of 703.

Verifying distances

I’m double-checking the total sum by validating the distances from 1986 to each number. Both segments add up to 703, resulting in a total of 1406.

Thought about 最小值求解 for 11 seconds

回答

分析思路:

要使S(n)=k=19492023nk\displaystyle S(n)=\sum_{k=1949}^{2023}|n-k|最小,一般应取nn为这些整数的中位数。因为从1949194920232023共有

20231949+1=752023 - 1949 + 1 = 75

个整数,个数为奇数,因此中位数就是这7575个数中排在第 75+12=38\frac{75+1}{2} = 38 位的那个数。

具体定位中位数:

  • 第 1 个数是 19491949
  • 第 2 个数是 19501950
  • \dots
  • 第 38 个数是 1949+381=1949+37=19861949 + 38 - 1 = 1949 + 37 = 1986

因此,当 n=1986n=1986 时,S(n)\displaystyle S(n) 取得最小值。


计算最小值:

将求和拆分为两部分:

  1. kk19491949 增加到 19861986 时,1986k=1986k|1986 - k| = 1986 - k
  2. kk19871987 增加到 20232023 时,1986k=k1986|1986 - k| = k - 1986

于是

S(1986)=k=19491986(1986k)+k=19872023(k1986).S(1986) = \sum_{k=1949}^{1986}(1986 - k) + \sum_{k=1987}^{2023}(k - 1986).
  1. 第一部分:
k=19491986(1986k)=m=019861949m=m=037m=0+1+2++37=37382=703.\sum_{k=1949}^{1986}(1986 - k) = \sum_{m=0}^{1986-1949} m = \sum_{m=0}^{37} m = 0 + 1 + 2 + \dots + 37 = \frac{37 \cdot 38}{2} = 703.
  1. 第二部分:
k=19872023(k1986)=m=120231986m=m=137m=1+2++37=37382=703.\sum_{k=1987}^{2023}(k - 1986) = \sum_{m=1}^{2023-1986} m = \sum_{m=1}^{37} m = 1 + 2 + \dots + 37 = \frac{37 \cdot 38}{2} = 703.

因此

S(1986)=703+703=1406.S(1986) = 703 + 703 = 1406.

结论:

  • 使 n1949+n1950++n2023\displaystyle |n-1949| + |n-1950| + \dots + |n-2023| 取最小值的 nn1986
  • 最小值为 1406

分享这个问答