10003 Cutting Sticks(区间dp)
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)的更多相关文章
- UVA 10003 Cutting Sticks 区间DP+记忆化搜索
UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...
- uva 10003 Cutting Sticks(区间DP)
题目连接:10003 - Cutting Sticks 题目大意:给出一个长l的木棍, 再给出n个要求切割的点,每次切割的代价是当前木棍的长度, 现在要求输出最小代价. 解题思路:区间DP, 每次查找 ...
- UVA 10003 Cutting Sticks(区间dp)
Description Cutting Sticks You have to cut a wood stick into pieces. The most affordable company ...
- 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部分,使得总切割费用最小.每次切割的费用 ...
随机推荐
- C# 实现磁性窗体
可以实现窗体的 吸附 移动 分离 using System; using System.Drawing; using System.Collections.Generic; using Sys ...
- rownum(转载)
对于 Oracle 的 rownum 问题,很多资料都说不支持>,>=,=,between...and,只能用以上符号(<.<=.!=),并非说用>,>=,=,be ...
- Sublime Text3中最常用的快捷键
ctrl+D 选词快捷键 反复按这快捷键,可以方便的向下选择相同的词~ alt + shift +2 分2屏 数字为几就是几屏 Alt + F3 可以一次性选择一个文件里面的所有相同的文本进行编辑 ...
- java中常用的数据加密算法
以下为加密的工具类: import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; im ...
- Javascript实现DIV的隐藏和出现
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- 20个经典bootsrtap后台html网站模板推荐
今天为大家推荐20款不同风格的Bootstrap后台管理模板,每一款都经典可用,能预览和下载,保证让你挑得眼花缭乱. 1,Simpli flag蓝色 Simpli Flat蓝色管理模板是一款采用Fla ...
- 解决magento保存产品时耗时很长的问题
以前我在更新产品属性值(拿price为例)的时候,通常会这样做: foreach($product_ids as $id){ $product = Mage::getModel('catalog/pr ...
- Azure Traffic Manager 现可与 Azure 网站集成!
编辑人员注释:本文章由 WindowsAzure 网站团队高级专家级工程师 Jim Cheshire撰写. AzureTraffic Manager 已经推出有一段时间,这是一种跨多个区域管理网 ...
- Element DOM Tree jQuery plugin – Firebug like functionality | RockingCode
Element DOM Tree jQuery plugin – Firebug like functionality | RockingCode Element DOM Tree jQuery pl ...
- css3的一些属性
以前还没有注意过css的一些属性,近期发现有一些样式很好用,现在整理一遍. CSS3 动画属性 @keyframes : 规定动画 可以通过keyframes 改变一个块的样式当然这是要配合anima ...