Files
Data-Structure/Algorithm/DP-DynamicProgramming/Linear-DP/Interval-DP/UVa10003CuttingSticks.cpp
2025-09-15 22:16:09 +08:00

39 lines
1.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include <vector>
using namespace std;
int main(){
int len, n;
cin >> len >> n;
vector<int> a(n + 2);
vector<vector<int>> dp(n + 2, vector<int>(n + 2, 0));
//设置dp[i][j]为切割木棍[i, j]段 0|---1|-----2|----3|--4| 最少所需的费用 a[0] = 0 a[1] = x,,, a[n + 1] = len
a[0] = 0; a[n + 1] = len;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
//a[i]是分点
/*这个不对
for(int i = 0; i <= n + 1; i++){
for(int j = i; j <= n + 1 ;j++){
dp[i][j] = 114514;
for(int k = i + 1; k < j; k++){
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + a[j] - a[i]);
}
if(dp[i][j] == 114514) dp[i][j] = 0;
}
} */
// 区间 DP按长度递增
for(int l = 2; l <= n + 1; l++){ // 区间长度至少2
for(int i = 0; i + l <= n + 1; i++){
int j = i + l;
dp[i][j] = INT_MAX;
for(int k = i + 1; k < j; k++){
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + a[j] - a[i]);
}
if(dp[i][j] == INT_MAX) dp[i][j] = 0;
}
}
cout << dp[0][n + 1] << endl;
return 0;
}