# Service Stations on a Line: Consecutive Groups and Medians ## Turn placement into grouping Sort the coordinates as $x_1\le\cdots\le x_N$ . In station order, each station can serve a consecutive interval of homes; distance ties can also be assigned consecutively. For any fixed group, a median minimizes the sum of absolute distances. Thus, an optimum exists with every station at a home coordinate. We may use exactly $M$ nonempty groups. If a station serves no home, $M\le N$ lets us split a group containing at least two homes and add a station without increasing the cost. Duplicate coordinates do not affect this argument. Let $P_i=\sum_{j=1}^ix_j$ and $P_0=0$ . The minimum cost of serving $[l,r]$ with one station is $$C(l,r)=x_q(q-l+1)-(P_q-P_{l-1})+(P_r-P_q)-x_q(r-q),\qquad q=\lfloor(l+r)/2\rfloor.$$ Prefix sums evaluate it in constant time. ## Dynamic programming and monotone decisions Let $D_t(r)$ be the minimum cost of splitting the first $r$ homes into $t$ groups. Set $D_0(0)=0$ and every other infeasible state to infinity. Then $$D_t(r)=\min_{t-1\le j