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.

题意:给定一段len长的木棍,有n个切割点,每个切割点切掉的花费是当前切割点所在木棍的长度,求最少的花费。

思路:这题我是把每个木棍分成已经切割好的状态,在从切割好进行复原,复原过程中用区间dp的方法记录花费。

状态转移方程为dp[i][j] = min{dp[i][k] + dp[k + 1][j] + he(当前要复原的长度)}。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int len, n, strick[55], i, j, k, l, sb, sbb, dp[55][55]; int min(int a, int b) {
return a < b ? a : b;
}
int main() {
while (~scanf("%d", &len) && len) {
sbb = 0;
memset(dp, 0, sizeof(dp));
scanf("%d", &n);
for (i = 1; i <= n; i ++) {
scanf("%d", &sb);
strick[i] = sb - sbb;
sbb = sb;
}
strick[++ n] = len - sb;
for (l = 1; l < n; l ++) {
for (i = 1; i <= n - l; i ++) {
j = i + l;
int sb = 999999999;
int he = 0;
for (k = i; k <= j; k ++)
he += strick[k];
for (k = i; k < j; k ++) {
sb = min(dp[i][k] + dp[k + 1][j] + he, sb);
}
dp[i][j] = sb;
}
}
printf("The minimum cutting is %d.\n", dp[1][n]);
}
return 0;
}

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. UVA 10003 Cutting Sticks(区间dp)

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

  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. WinSock网络编程基础(1)

    记录学习windows网络编程过程中遇到的问题和相关笔记 基本概念: Socket: socket起源于UNIX,Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.基于&qu ...

  2. BZOJ 1565: [NOI2009]植物大战僵尸( 最小割 )

    先拓扑排序搞出合法的, 然后就是最大权闭合图模型了.... --------------------------------------------------------------------- ...

  3. Labview学习之程序Web发布

    Labview学习之程序Web发布 1. LabVIEW Web服务器     在LabVIEW开发环境中,自身带了一个已连接好的Web服务器.LabVIEW Web服务器除了与其他Web服务器一样能 ...

  4. B实习面试

    1. 多态和继承关系,继承的几种实现机制? 实现多态,有二种方式,覆盖,重载. 覆盖,是指子类重新定义父类的虚函数的做法. 重载,是指允许存在多个同名函数,而这些函数的参数表不同(或许参数个数不同,或 ...

  5. linux杂记(十)what is BASH Shell

    first,what is shell?其实只要是碰过计算机的,对于OS(Operation System操作系统,不管是linux.unix.windows)有点概念的人大多都听过这个名词,因为只要 ...

  6. HTML5 调用手机相册和摄像头的方法并上传微信下测试通过

    <input type="file" capture="camera" accept="image/*" id="camer ...

  7. IBM HeapAnalyzer

    https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/W3b463571efc8_4f02_99af_3cbc0 ...

  8. 在PADS LAYOUT中如何隐藏不需要的鼠线?

    如下图示,将net GPR_0的鼠线隐藏. 鼠标右键,选择网络----选择你要隐藏的网络------右键选择view nets----点击对话框右边View List里你所选的网络-----在右下角t ...

  9. SPOJ 220 Relevant Phrases of Annihilation(后缀数组+二分答案)

    [题目链接] http://www.spoj.pl/problems/PHRASES/ [题目大意] 求在每个字符串中出现至少两次的最长的子串 [题解] 注意到这么几个关键点:最长,至少两次,每个字符 ...

  10. CH Round #57 - Story of the OI Class 凯撒密码

    很有意思的一道题目 考场上想的是HASH成一个整数,把末位asicc码值*1,依次乘*10,得到一个整数,然后利用等差性.唯一性快排Nlogn乱搞的 证明如下: 对于明文abcde 密文 bcdef ...