UVA 10003 - Cutting Sticks

来源:互联网 发布:网络十大神曲 编辑:程序博客网 时间:2024/06/18 08:08
#include<iostream>#include<map>#include<string>#include<cstring>#include<cstdio>#include<cstdlib>#include<cmath>#include<queue>#include<vector>#include<algorithm>using namespace std;int a[100];int n;int dp[1010][1010];int dfs(int s,int e){int i;if(dp[s][e]>-1)return dp[s][e];i=upper_bound(a,a+n,s)-a;if(i==n||a[i]>=e)dp[s][e]=0;for(;i<n&&a[i]<e;i++)if(dp[s][e]!=-1)dp[s][e]=min(dfs(s,a[i])+dfs(a[i],e)+e-s,dp[s][e]);elsedp[s][e]=dfs(s,a[i])+dfs(a[i],e)+e-s;return dp[s][e];}int main(){int i,l;while(cin>>l&&l){cin>>n;for(i=0;i<n;i++)cin>>a[i];memset(dp,-1,sizeof(dp));printf("The minimum cutting is %d.\n",dfs(0,l));}}

 Cutting Sticks 

You have to cut a wood stick into pieces. The most affordable company, TheAnalog Cutting Machinery, Inc. (ACM), charges money according to the length ofthe stick being cut. Their procedure of work requires that they only make onecut at a time.

It is easy to notice that different selections in the order ofcutting can led to different prices. For example, consider a stick of length 10meters that has to be cut at 2, 4 and 7 meters from one end. There are severalchoices. One can be cutting first at 2, then at 4, then at 7. This leads to aprice of 10 + 8 + 6 = 24 because the first stick was of 10 meters, theresulting 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 isa better price.

Your boss trusts your computer abilities to find out theminimum cost for cutting a given stick.

Input 

The input will consist of several input cases. The firstline of each test case will contain a positive numberl that represents thelength of the stick to be cut. You can assume l < 1000. The next line willcontain the numbern (n < 50) of cuts to be made.

The next line consistsof n positive numbers ci (0 <ci < l) representing the places where thecuts 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 theoptimal solution of the cutting problem, that is the minimum cost of cuttingthe given stick. Format the output as shown below.

Sample Input 

100325 50 751044 5 7 80

Sample Output 

The minimum cutting is 200.The minimum cutting is 22.



Miguel Revilla
2000-08-21

0 0
原创粉丝点击