题解:P13600 [NWRRC 2022] Computer Network

· · 题解

题目大意

题目传送门

题目分析

我们将 d_i 升序排序之后,最小总延迟的计算公式为:

\text{total} = \sum_{i=1}^n d_i \times \left(\left\lfloor \frac{n - i}{k} \right\rfloor + 1\right)

::::info[证明] 通过归纳法可证明该分配方式最优:

  1. 假设对 n=m 成立,当 n=m+1 时:
    • 新增节点必须放在当前最浅的非满层
    • 乘数 \left\lfloor \frac{(m+1)-i-1}{k} \right\rfloor +1 可以确保总是选择最优位置。

代码实现

#include <bits/stdc++.h>

using namespace std;

int n, k;
int d[100];

signed main() {
    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        cin >> d[i];
    }
    sort(d, d + n);
    int tot = 0;
    for (int i = 0; i < n; ++i) {
        tot += d[i] * ((n - i - 1) / k + 1);
    }
    cout << tot;
    return 0;
}