Run ID 作者 问题 语言 测评结果 时间 内存 代码长度 提交时间
31108 diandian 山区建小学 C++ 解答错误 1 MS 1272 KB 861 2023-11-14 22:00:34

Tests(0/1):


#include <bits/stdc++.h> using namespace std; int n, m; int d[505]; int dis[505][505]; int dp[505][505]; int cal (int x, int y){ int mid = (x + y) / 2, ans = 0; for (int i = x; i <= y; ++ i) ans += abs(d[i] - d[mid]); } int main() { cin >> m >> n; for (int i = 2; i <= m; ++ i){ scanf ("%d", &d[i]); d[i] += d[i - 1]; } for (int i = 1; i <= m; ++ i) for (int j = i; j <= m; ++ j) dis [i][j] = cal(i, j); memset(dp, 0x3f, sizeof(dp)); for (int i = 1; i <= m; ++ i){ for (int j = 1; j <= n && j <= i; ++ j){ if (j == 1) dp[i][j] = dis[1][i]; else{ for (int k = 1; k < i; ++ k){ dp[i][j] = min(dp[i][j], dp[k][j - 1] + dis[k + 1][i]); } } } } cout << dp[m][n]; return 0; }


测评信息: