Description

 
 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 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.

 状态转移方程:d(i,j)=min(d(i,k)+d(k,j)+a[j]-a[i]) (i<k<j)

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn=;
const int INF=;
int d[maxn][maxn],a[maxn],l,n;
inline int min(int a,int b){return a<b?a:b;} int dp(int i,int j)
{
if(i+==j) return d[i][j]=;
if(d[i][j]!=-) return d[i][j];
d[i][j]=INF;
for(int k=i+;k<j;k++)
d[i][j]=min(d[i][j],dp(i,k)+dp(k,j)+a[j]-a[i]);
return d[i][j];
}
int main()
{
while(~scanf("%d",&l),l)
{
scanf("%d",&n);
memset(d,-,sizeof(d));
for(int i=;i<=n;i++) scanf("%d",a+i);
a[]=;a[n+]=l;
printf("The minimum cutting is %d.\n",dp(,n+));
}
return ;
}

UVA 10003 Cutting Sticks(区间dp)的更多相关文章

  1. UVA 10003 Cutting Sticks 区间DP+记忆化搜索

    UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...

  2. uva 10003 Cutting Sticks(区间DP)

    题目连接:10003 - Cutting Sticks 题目大意:给出一个长l的木棍, 再给出n个要求切割的点,每次切割的代价是当前木棍的长度, 现在要求输出最小代价. 解题思路:区间DP, 每次查找 ...

  3. 10003 Cutting Sticks(区间dp)

      Cutting Sticks  You have to cut a wood stick into pieces. The most affordable company, The Analog ...

  4. uva 10003 Cutting Sticks 【区间dp】

    题目:uva 10003 Cutting Sticks 题意:给出一根长度 l 的木棍,要截断从某些点,然后截断的花费是当前木棍的长度,求总的最小花费? 分析:典型的区间dp,事实上和石子归并是一样的 ...

  5. UVA 10003 Cutting Sticks

    题意:在给出的n个结点处切断木棍,并且在切断木棍时木棍有多长就花费多长的代价,将所有结点切断,并且使代价最小. 思路:设DP[i][j]为,从i,j点切开的木材,完成切割需要的cost,显然对于所有D ...

  6. UVa 10003 - Cutting Sticks(区间DP)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. uva 10003 Cutting Sticks (区间dp)

    本文出自   http://blog.csdn.net/shuangde800 题目链接:  打开 题目大意 一根长为l的木棍,上面有n个"切点",每个点的位置为c[i] 要按照一 ...

  8. UVA 10003 Cutting Sticks 切木棍 dp

    题意:把一根木棍按给定的n个点切下去,每次切的花费为切的那段木棍的长度,求最小花费. 这题出在dp入门这边,但是我看完题后有强烈的既是感,这不是以前做过的石子合并的题目变形吗? 题目其实就是把n+1根 ...

  9. UVA - 10003 Cutting Sticks(切木棍)(dp)

    题意:有一根长度为L(L<1000)的棍子,还有n(n < 50)个切割点的位置(按照从小到大排列).你的任务是在这些切割点的位置处把棍子切成n+1部分,使得总切割费用最小.每次切割的费用 ...

随机推荐

  1. js生成指定范围内随机数

    其现方法的核心是JavaScript的Math对象.代码如下: <!DOCTYPE html> <html lang="en"> <head> ...

  2. (71)Received empty response from Zabbix Agent问题解决

    刚接触zabbix新手少部分会出现如下错误: Received empty response from Zabbix Agent at [192.168.1.2]. Assuming that age ...

  3. Ansible的使用和模块化深入

    Ansible配置 配置文件:/etc/ansible/ansible.cfg [default] 默认配置 inventory = /etc/ansible/hosts主机清单 library = ...

  4. H5bulider中的微信支付配置注意事项

    一.云打包安卓自定义证书的生成: 签名算法名称: SHA1withRSA主体公共密钥算法:1024 位 RSA 密钥密钥库类型:JKS 1.下载JDK1.6安装,切换到bin目录,打开命令行: 2.生 ...

  5. 【mysql】[Err]1267 - Illegal mix of collations(utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation ‘=

    ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

  6. (转)减少oracle sql回表次数 提高SQL查询性能

    要写出高效的SQL,那么必须必须得清楚SQL执行路径,介绍如何提高SQL性能的文章很多,这里不再赘述,本人来谈谈如何从 减少SQL回表次数 来提高查询性能,因为回表将导致扫描更多的数据块. 我们大家都 ...

  7. Mysql源码编译安装&主从复制

    一)camke源码编译安装mysql 1)创建软件安装目录software [root@master software]# ls cmake-2.8.8.tar.gz mysql-5.5.32.tar ...

  8. UVA10779Collectors Problem

    uva 10779 Collectors Problem Some candy manufacturers put stickers into candy bar packages. Bob and ...

  9. python基础学习笔记——异常处理

    异常处理流程图 一,异常和错误 part1:程序中难免出现错误,而错误分成两种 1.语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) #语法错误示范一 if #语法 ...

  10. Django Form two

    Django_Form: 1.class TeacherNewForm(Form): username = fields.CharField( required=True, error_message ...