### Problem Statement There are $N$ houses number...

Creato il: 12 luglio 2025

Risposto usando O3 da Chat01

Domanda

Problem Statement

There are NN houses numbered from 11 to NN on a number line. House ii is located at coordinate XiX_i. Multiple houses may be located at the same coordinate.

You place MM base stations at arbitrary real coordinates on the number line. Then, you set a non-negative integer signal strength for each base station.

When the signal strength of a base station is set to xx, The signal from that base station reaches a house if and only if the distance between the base station and the house is at most x2\displaystyle\frac{x}{2}. Particularly, when x=0x=0, the signal reaches only houses located at the same coordinate as the base station.

Find the minimum possible sum of signal strengths when the positions and signal strengths of the base stations are set such that at least one base station's signal reaches every house.

It can be proved that the answer is an integer for any input satisfying the constraints.### Constraints

  • 1MN5×1051 \leq M \leq N \leq 5 \times 10^5
  • 1Xi10121 \leq X_i \leq 10^{12} (1iN1 \leq i \leq N)
  • All input values are integers.### Output

Output the answer as an integer in one line.
C++ code 用中文回答我,仔细思考

Pensiero

Explaining the problem

The problem is about minimizing the total signal strength of M base stations to cover N houses. The signal strength at each station is represented by xx, and the effective coverage radius is x/2x/2. The goal is to minimize the sum of all xx's, which is framed as finding up to M intervals centered at selected positions that cover all houses. Each interval wants to minimize its coverage radius rr, and we multiply by 2 to get x=2rx = 2r. I need to minimize x\sum x.

Explaining the algorithm

The problem involves minimizing total signal strengths for M base stations covering N houses. We want to place the base stations efficiently, considering that the signal strength is related to the coverage radius.

The optimal intervals are contiguous segments of points, and to minimize the sum of lengths, we can partition the sorted coordinates into at most M segments. The signal strength for each segment will be the difference between the maximum and minimum of that segment, doubled (since x = (max - min) * 2). This situation mirrors optimizing disk coverage—it's similar to partitioning points while minimizing the sum of segment lengths.

Thought for 18 seconds

Risposta

思路说明(中文)

  1. 将房屋坐标排序
    设排序后序列为 x1x2xNx_1\le x_2\le\cdots\le x_N

  2. “一条基站线”下的代价
    如果只有一座基站覆盖所有房屋,则最小信号强度为

    xNx1 x_N-x_1

    ——把基站放在 x1+xN2\tfrac{x_1+x_N}{2} 处即可。
    这是因为每段的信号强度 ss 对应的覆盖半径是 s2\tfrac{s}{2}, 要让最左与最右两栋房屋都被覆盖,半径至少是 xNx12\tfrac{x_N-x_1}{2},从而 s=xNx1s=x_N-x_1

  3. 拆分成多段的收益
    若我们把序列在若干位置“断开”,每段的花费是
    (段右端坐标段左端坐标)(\text{段右端坐标}-\text{段左端坐标})
    全部段花费之和 = (xNx1)(x_N-x_1) 减去 每一次断开造成的“空隙”大小。
    记相邻房屋间的间隔

    di=xi+1xi,i=1..N1. d_i = x_{i+1}-x_i,\quad i=1..N-1.

    若要放置 MM 个基站,就能断开最多 M1M-1 处,于是总花费为

    (xNx1)    选出的 (M1) 个最大 di 的和. (x_N-x_1) \;-\; \text{选出的 }(M-1)\text{ 个最大 }d_i\text{ 的和}.
  4. 算法步骤

    • 排序 XXO(NlogN)O(N\log N))。
    • 计算全部间隔 did_iO(N)O(N))。
    • did_i 按降序排序,取前 M1M-1 个求和(O(NlogN)O(N\log N))。
    • 结果 = (xNx1)上述求和(x_N-x_1) - \text{上述求和}
      MNM\ge N 时可以让每栋房子独占一个基站且信号强度为 00,答案自然为 00
  5. 复杂度分析

    • 排序两次:整体 O(NlogN)O(N\log N)
    • 额外内存 O(N)O(N)

cpp
#include <bits/stdc++.h> using namespace std; using int64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, M; if (!(cin >> N >> M)) return 0; vector<int64> x(N); for (int i = 0; i < N; ++i) cin >> x[i]; sort(x.begin(), x.end()); // 步骤 1 if (M >= N) { // 特判:基站足够多 cout << 0 << '\n'; return 0; } vector<int64> gaps; gaps.reserve(N - 1); for (int i = 0; i < N - 1; ++i) // 步骤 2 gaps.push_back(x[i + 1] - x[i]); sort(gaps.rbegin(), gaps.rend()); // 步骤 3:降序 int64 save = 0; // 可以节省的总空隙 for (int i = 0; i < M - 1; ++i) save += gaps[i]; int64 ans = (x.back() - x.front()) - save;// 步骤 4 cout << ans << '\n'; return 0; }

解释小结

  • 核心:把“覆盖所有房屋的一段”拆成若干段,节省的正是拆开的最大空隙。
  • 答案(xmaxxmin)(x_{\max}-x_{\min})-“前 M1M-1 大的相邻空隙之和”。
  • 正确性:分段之间绝不会重叠,否则可收缩信号强度;段内最优就是让基站居中。
  • 时间/空间O(NlogN)O(N\log N) / O(N)O(N),可轻松通过 N5×105N\le5\times10^5

Condividi questa Q&A