70062 - 2025青少年编程挑战赛入门组完善2
统计题目(材料题)
(2)石子合并 有N堆石子排成一排,每堆石子有一定的数量。现在要将这些石子合并成一堆,每次只能合并相邻的两堆石子,合并的代价是这两堆石子的数量之和。问将所有石子合并成一堆的最小总代价是多少?
试补全程序:
01 #include <iostream>
02 #define INF 2147483647
03 using namespace std;
04 const int M = 305;
05 int stones[M], pre_sum[M], dp[M][M];
06
07 int main() {
08 int n;
09 cin >> n;
10 for (int i = 1; i <= n; i++) {
11 cin >> stones[i];
12 ① ;
13 }
14 for (int len = 2; len <= n; len++) {
15 for (int i = 1; i + len - 1 <= n; i++) {
16 ② ;
17 ③ ;
18 for (int k = i; k < j; k++) {
19 int cost = ④ ;
20 if (cost < dp[i][j]) {
21 dp[i][j] = cost;
22 }
23 }
24 }
25 }
26 cout << ⑤ << endl;
27 return 0;
28 }

关注我们