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.
Cutting Sticks |
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 l that 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.
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int a[60],dp[60][60];
const int inf=1<<30;
int main()
{
int n,len;
while(scanf("%d",&len),len){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
a[0]=0,a[n+1]=len;
for(int l=2;l<=n+1;l++)
for(int i=0;i+l<=n+1;i++){
dp[i][i+l]=inf;
for(int k=i+1;k<i+l;k++)
dp[i][i+l]=min(dp[i][i+l],dp[i][k]+dp[k][i+l]+a[i+l]-a[i]);
}
printf("The minimum cutting is %d.\n",dp[0][n+1]);
}
return 0;
}
记忆化搜索代码:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int n,len;
int a[60],dp[1010][1010];
const int inf=1<<30;
int dfs(int s,int e){
int &ans=dp[s][e];
bool flag=true;
if(ans!=inf) return ans;
for(int i=1;i<=n;i++)
if(a[i]>s&&a[i]<e){
ans=min(ans,dfs(s,a[i])+dfs(a[i],e)+e-s);
flag=false;
}
if(flag) return ans=0;
return ans;
}
int main()
{
while(scanf("%d",&len),len){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
for(int i=0;i<=len;i++)
for(int j=0;j<=len;j++)
dp[i][j]=inf;
printf("The minimum cutting is %d.\n",dfs(0,len));
}
}
暴力枚举区间的,因此数组要开len*len,并且复杂度是O(len*len)
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int n,len;
int a[60],dp[60][60];
const int inf=1<<30;
int dfs(int s,int e){
int &ans=dp[s][e];
if(s+1==e) return ans=0;
if(ans!=inf) return ans;
for(int i=s+1;i<e;i++)
ans=min(ans,dfs(s,i)+dfs(i,e)+a[e]-a[s]);
return ans;
}
int main()
{
while(scanf("%d",&len),len){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
a[0]=0,a[n+1]=len;
for(int i=0;i<=n+1;i++)
for(int j=0;j<=n+1;j++)
dp[i][j]=inf;
printf("The minimum cutting is %d.\n",dfs(0,n+1));
}
}
这个空间和时间复杂度一下降到了O(n*n),是个不错的优化。