题解:P13600 [NWRRC 2022] Computer Network
Clouds_dream · · 题解
题目大意
题目传送门
题目分析
我们将
::::info[证明] 通过归纳法可证明该分配方式最优:
-
- 假设对
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;
}