UVA10003 Cutting Sticks
Posted 嘒彼小星
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10003 Cutting Sticks相关的知识,希望对你有一定的参考价值。
https://odzkskevi.qnssl.com/e17d84412b7ea3a42b6503109d6dfbc1?v=1508053531
【题解】
裸区间dp,注意一堆细节
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstdlib> 5 #include <cstring> 6 #include <cmath> 7 #define max(a, b) ((a) > (b) ? (a) : (b)) 8 #define min(a, b) ((a) < (b) ? (a) : (b)) 9 inline void swap(int &a, int &b) 10 { 11 int tmp = a;a = b;b = tmp; 12 } 13 inline void read(int &x) 14 { 15 x = 0;char ch = getchar(), c = ch; 16 while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar(); 17 while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar(); 18 if(c == ‘-‘)x = -x; 19 } 20 21 const int INF = 0x3f3f3f3f; 22 const int MAXN = 50 + 10; 23 24 int dp[MAXN][MAXN], po[MAXN], n, L; 25 26 int main() 27 { 28 while(scanf("%d", &L) != EOF && L) 29 { 30 read(n); 31 for(register int i = 1;i <= n;++ i) 32 read(po[i]); 33 std::sort(po + 1, po + 1 + n); 34 for(register int i = 0;i <= n + 1;++ i) 35 dp[i][i] = 0, dp[i][i + 1] = 0; 36 po[n + 1] = L; 37 for(register int k = 3;k <= n + 2;++ k) 38 { 39 for(register int i = 0;i <= n + 1;++ i) 40 { 41 int j = i + k - 1; 42 dp[i][j] = INF; 43 if(j > n + 1)continue; 44 for(register int m = i + 1;m < j;++ m) 45 dp[i][j] = min(dp[i][j], dp[i][m] + dp[m][j] + po[j] - po[i]); 46 } 47 } 48 printf("The minimum cutting is %d.\n", dp[0][n + 1]); 49 } 50 return 0; 51 }
以上是关于UVA10003 Cutting Sticks的主要内容,如果未能解决你的问题,请参考以下文章
UVA-10003 Cutting Sticks (区间DP)