Uva--10003(区间动规,四边形不等式)
2014-08-27 15:19:59
| Cutting Sticks |
You have to cut a wood stick into pieces. The most affordable company, The Analog Cutting Machinery, Inc. (ACM), charges money according to the length of the stick being cut. Their procedure of work requires that they only make one cut at a time.
It is easy to notice that different selections in the order of cutting can led to different prices. For example, consider a stick of length 10 meters that has to be cut at 2, 4 and 7 meters from one end. There are several choices. One can be cutting first at 2, then at 4, then at 7. This leads to a price of 10 + 8 + 6 = 24 because the first stick was of 10 meters, the resulting of 8 and the last one of 6. Another choice could be cutting at 4, then at 2, then at 7. This would lead to a price of 10 + 4 + 6 = 20, which is a better price.
Your boss trusts your computer abilities to find out the minimum cost for cutting a given stick.
Input
The input will consist of several input cases. The first line of each test case will contain a positive number lthat represents the length of the stick to be cut. You can assume l < 1000. The next line will contain the number n (n < 50) of cuts to be made.
The next line consists of n positive numbers ci ( 0 < ci < l) representing the places where the cuts have to be done, given in strictly increasing order.
An input case with l = 0 will represent the end of the input.
Output
You have to print the cost of the optimal solution of the cutting problem, that is the minimum cost of cutting the given stick. Format the output as shown below.
Sample Input
100 3 25 50 75 10 4 4 5 7 8 0
Sample Output
The minimum cutting is 200. The minimum cutting is 22.
思路:非常典型的一道DP题,也是小白书上的例题,可以用记忆化搜索,也可用区间动规(O(n^3)),最高效的算法是用四边形不等式优化的区间动规(O(n^2))。
dp[i][j]表示切割小木棍 i ~ j 的最少费用。
记忆化搜索版(285ms):
1 /************************************************************************* 2 > File Name: e.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Tue 29 Jul 2014 09:57:28 AM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 const int INF = 1000000000; 16 17 int L,n,c[55],dp[55][55]; 18 19 int Solve(int x,int y){ 20 if(y - x == 1) 21 return 0; 22 if(dp[x][y]) return dp[x][y]; 23 int tmin = INF; 24 for(int k = x + 1; k < y; ++k) 25 tmin = min(tmin,Solve(x,k) + Solve(k,y)); 26 return (dp[x][y] = tmin + c[y] - c[x]); 27 } 28 29 int main(){ 30 while(scanf("%d",&L) == 1 && L){ 31 scanf("%d",&n); 32 for(int i = 1; i <= n; ++i) 33 scanf("%d",&c[i]); 34 memset(dp,0,sizeof(dp)); 35 // dp[i][j] = min{dp[i][k] + dp[k][j]) | i < k < j} + j - i; 36 // 对每个切点,都考虑其他n - 1个切点,总状态数: n^2 37 c[0] = 0; 38 c[n + 1] = L; 39 printf("The minimum cutting is %d.\n",Solve(0,n + 1)); 40 } 41 return 0; 42 }
区间动规版(83ms):
1 /************************************************************************* 2 > File Name: 10003.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Wed 27 Aug 2014 03:26:35 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 const int INF = 1 << 30; 16 17 int dp[55][55]; 18 int L,n,c[55]; 19 20 int main(){ 21 while(scanf("%d",&L) == 1 && L){ 22 scanf("%d",&n); 23 for(int i = 1; i <= n; ++i) 24 scanf("%d",&c[i]); 25 c[0] = 0; 26 c[n + 1] = L; 27 for(int i = 0; i < n + 1; ++i){ 28 dp[i][i + 1] = 0; 29 } 30 for(int len = 2; len <= n + 1; ++len){ 31 for(int i = 0; i <= n + 1 - len; ++i){ 32 int j = i + len; 33 int res = INF; 34 for(int k = i + 1; k < j; ++k){ 35 res = min(res,dp[i][k] + dp[k][j]); 36 } 37 dp[i][j] = res + c[j] - c[i]; 38 } 39 } 40 printf("The minimum cutting is %d.\n",dp[0][n + 1]); 41 } 42 return 0; 43 }
四边形不等式优化的区间动规(53ms),借鉴了传说中的快速读入,再加上一些丧病的优化 QAQ,效率rank终于#7 好感动ing TAT。
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 const int INF = 1 << 30; 6 7 inline int Read(){ 8 int x = 0; char ch = getchar(); 9 while(ch < '0' || ch > '9') ch = getchar(); 10 while(ch >= '0' && ch <= '9'){x = x*10 + ch - '0'; ch = getchar();} 11 return x; 12 } 13 14 int dp[51][51]; 15 int S[51][51]; 16 int L,n,c[51],res,j,len,i,k; 17 18 int main(){ 19 for( i = 1; i <= 50; ++i) 20 S[i][i + 1] = i + 1; 21 S[0][1] = 1; 22 while(L = Read()){ 23 n = Read(); 24 for(i = 1; i <= n; ++i) 25 c[i]= Read(); 26 c[n + 1]= L; 27 memset(dp[0],0,sizeof(dp[0])); 28 for(len = 2; len <= n + 1; ++len){ 29 for(i = 0,j = i + len; i <= n + 1 - len; ++i){ 30 dp[i][j] = INF; 31 for(k = S[i][j - 1]; k <= S[i + 1][j]; ++k){ 32 res = dp[i][k] + dp[k][j]; 33 if(res < dp[i][j]){ 34 dp[i][j] = res; 35 S[i][j] = k; 36 } 37 } 38 dp[i][j] += c[j] - c[i]; 39 j++; 40 } 41 } 42 printf("The minimum cutting is %d.\n",dp[0][n + 1]); 43 } 44 return 0; 45 }

浙公网安备 33010602011771号