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. CocoaPods 安装和使用

    CocoaPods的安装 >1. 打开终端, 输入 gem sources -remove https://rubygems.org/ >2. 再输入 gem sources -a htt ...

  2. <jsp:directive.page>标签

    directive 英 [dɪ'rektɪv; daɪ-] 美 [daɪ'rɛktɪv] n. 指示:指令 adj. 指导的:管理的 等效于 <%page import="com.ct ...

  3. trangle

    #include<iostream> #include<algorithm> using namespace std; int main() { int a,b,c; whil ...

  4. 框架开发(三)---smarty整合

    一 smarty 是什么 Smarty是一个PHP的模板引擎.更明确来说,它可以帮助开发者更好地 分离程序逻辑和页面显示.最好的例子,是当程序员和模板设计师是不同的两个角色的情况,而且 大部分时候都不 ...

  5. eclipse更改主题

    长期使用eclipse,导致视觉疲劳,就想着能否换个主题调节调节. 通过设置window>preferences>appearance设置theme,貌似不起作用. 一查,发现一个绝佳的网 ...

  6. Mysql 权限修改何时生效

    首先权限是记录在表中的,所以如果我们要修改权限只要修改表中的数据就可以了! 方法 1 grant ,revoke,set password,rename user .......等等 2 insert ...

  7. 阿里云ECS每天一件事D7:安装tomcat8.0

    这一D,跨越了几个月啊,人是越来越懒,集中写一些,就懒得再记录了.也是因为测试需要,搭建个jsp的服务环境,只是测试,考虑用tomcat就够了. 在Tomcat官网下载最新Core版本,下载之后,将文 ...

  8. Qt全屏显示窗口、子窗口的相关函数

    Qt全屏显示函数         window.showFullScreen() Qt最大化显示函数         window.showMaximized() Qt最小化显示函数         ...

  9. Oracle 11g RAC database on ASM, ACFS or OCFS2

    I see a lot of questions on shared file systems that can be used when people move from single instan ...

  10. php操作xml详解

    XML是一种流行的半结构化文件格式,以一种类似数据库的格式存储数据.在实际应用中,一些简单的.安全性较低的数据往往使用 XML文件的格式进行存储.这样做的好处一方面可以通过减少与数据库的交互性操作提高 ...