现在的位置: 首页 > 综合 > 正文

UVA 10003 – Cutting Sticks

2019年11月10日 ⁄ 综合 ⁄ 共 1953字 ⁄ 字号 评论关闭
#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]);
		else
			dp[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 number
l that represents thelength of the stick to be cut. You can assume l < 1000. The next line willcontain the number
n (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 

100
3
25 50 75
10
4
4 5 7 8
0

Sample Output 

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


Miguel Revilla
2000-08-21

抱歉!评论已关闭.