UVA 10003 Cutting Sticks(区间dp)
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)的更多相关文章
- UVA 10003 Cutting Sticks 区间DP+记忆化搜索
UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...
- uva 10003 Cutting Sticks(区间DP)
题目连接:10003 - Cutting Sticks 题目大意:给出一个长l的木棍, 再给出n个要求切割的点,每次切割的代价是当前木棍的长度, 现在要求输出最小代价. 解题思路:区间DP, 每次查找 ...
- 10003 Cutting Sticks(区间dp)
Cutting Sticks You have to cut a wood stick into pieces. The most affordable company, The Analog ...
- uva 10003 Cutting Sticks 【区间dp】
题目:uva 10003 Cutting Sticks 题意:给出一根长度 l 的木棍,要截断从某些点,然后截断的花费是当前木棍的长度,求总的最小花费? 分析:典型的区间dp,事实上和石子归并是一样的 ...
- UVA 10003 Cutting Sticks
题意:在给出的n个结点处切断木棍,并且在切断木棍时木棍有多长就花费多长的代价,将所有结点切断,并且使代价最小. 思路:设DP[i][j]为,从i,j点切开的木材,完成切割需要的cost,显然对于所有D ...
- UVa 10003 - Cutting Sticks(区间DP)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 10003 Cutting Sticks (区间dp)
本文出自 http://blog.csdn.net/shuangde800 题目链接: 打开 题目大意 一根长为l的木棍,上面有n个"切点",每个点的位置为c[i] 要按照一 ...
- UVA 10003 Cutting Sticks 切木棍 dp
题意:把一根木棍按给定的n个点切下去,每次切的花费为切的那段木棍的长度,求最小花费. 这题出在dp入门这边,但是我看完题后有强烈的既是感,这不是以前做过的石子合并的题目变形吗? 题目其实就是把n+1根 ...
- UVA - 10003 Cutting Sticks(切木棍)(dp)
题意:有一根长度为L(L<1000)的棍子,还有n(n < 50)个切割点的位置(按照从小到大排列).你的任务是在这些切割点的位置处把棍子切成n+1部分,使得总切割费用最小.每次切割的费用 ...
随机推荐
- Bootstrap 提示工具(Tooltip)插件方法的用法
方法 下面是一些提示工具(Tooltip)插件中有用的方法: 方法 描述 实例 Options: .tooltip(options) 向元素集合附加提示工具句柄. $().tooltip(option ...
- C#数组协方差
对于任意两个“引用类型”A和B,如果存在从A到B的隐式引用转换或显式引用转换,则也一定存在从数组类型A[R]到数组类型B[R]的相同的引用转换,其中R可以是任何给定的“秩说明符”,但是这两个数组类型必 ...
- JDBC连接数据库报错:java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. ......
问题:Java程序使用JDBC连接MySQL数据库时,控制台报错如下: java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' ...
- C/C++基础知识:函数指针和指针函数的基本概念
[函数指针] 在程序运行中,函数代码是程序的算法指令部分,它们和数组一样也占用存储空间,都有相应的地址.可以使用指针变量指向数组的首地址,也可以使用指针变量指向函数代码的首地址,指向函数代码首地址的指 ...
- [JZOJ] 5837.Omeed
先摆出来这个式子 \[ score=A\sum S_i+B\sum S_i\times f(i) \] 先研究\(f\)函数(也就是Combo函数) 显然的有 \[ f(i)=P_i(f(i-1)+1 ...
- Linux常用快捷键以及如何查看命令帮助
1.1 Linux系统快速操作常用快捷键 快捷键名称 快捷作用 Ctrl + a 将光标移至行首 Ctrl + e 将光标移至行尾 Ctrl + u 前提光标在行尾,则清除当前行所有的内容(有空 ...
- uvm transaction modeling
1.what is transaction? network transactions tcp/ip wifi 3g/4g bus transactions amba-ahb/apb/axi pci/ ...
- Python基础(六)——面向对象编程
(1)定义类和实例 这一部分难得和 Java 较为一致,直接写个例子: class Stu: def __init__(self, name, id): # 构造方法 self.name = name ...
- Python使用ORM控制MongoDB(MongoEngine)
简介: MongoEngine是一个对象文档映射器(ODM),相当于一个基于SQL的对象关系映射器(ORM) pymongo来操作MongoDB数据库,但是直接把对于数据库的操作代码都写在脚本中,这会 ...
- Java中对象方法的调用过程&动态绑定(Dynamic Binding)
Java面向对象的最重要的一个特点就是多态, 而多态当中涉及到了一个重要的机制是动态绑定(Dynamic binding). 之前只有一个大概的概念, 没有深入去了解动态绑定的机理, 直到很多公司都问 ...