标签:style blog io color ar os for sp strong
#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 |
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.
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.
100 3 25 50 75 10 4 4 5 7 8 0
The minimum cutting is 200. The minimum cutting is 22.
标签:style blog io color ar os for sp strong
原文地址:http://blog.csdn.net/stl112514/article/details/40832277