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

分析:

1、solve(i, j)为切割小木棍i~j的最优费用。

2、设k(i<k<j),solve(i, j) = min{solve(i, k) + solve(k, j)} + a[j] - a[i]。

k是切割小木棍i~j费用最优的切割点,a[j] - a[i] 为小木棍i ~ j 的长度,即切割小木棍i~j的费用。

3、将小木棍的n个切割点标记为1~n,左端点为0,右端点为n + 1,则答案为solve(0, n + 1)。

4、注意L和n个切割点的位置都是positive number,所以可能是浮点数。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 50 + 10;
const int MAXT = 10000 + 10;
using namespace std;
double a[MAXN], dp[MAXN][MAXN];
double solve(int l, int r){
if(dcmp(dp[l][r], -1)) return dp[l][r];
if(r - l == 1) return dp[l][r] = 0.0;
double mi = 1e15;
for(int k = l + 1; k < r; ++k){
double tmp = solve(l, k) + solve(k, r);
if(dcmp(tmp, mi) == -1) mi = tmp;
}
return dp[l][r] = mi + a[r] - a[l];
}
int main(){
double L;
while(scanf("%lf", &L) == 1){
if(!dcmp(L, 0)) return 0;
int n;
scanf("%d", &n);
a[0] = 0.0;
for(int i = 1; i <= n; ++i){
scanf("%lf", &a[i]);
}
a[n + 1] = L;
for(int i = 0; i <= n + 1; ++i)
for(int j = 0; j <= n + 1; ++j)
dp[i][j] = -1;
printf("The minimum cutting is %g.\n", solve(0, n + 1));
}
return 0;
}

  

UVA - 10003 Cutting Sticks(切木棍)(dp)的更多相关文章

  1. UVA 10003 Cutting Sticks 切木棍 dp

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

  2. uva 10003 Cutting Sticks 【区间dp】

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

  3. UVA 10003 cuting sticks 切木棍 (区间dp)

    区间dp,切割dp[i][j]的花费和切法无关(无后效性) dp[i][j]表示区间i,j的花费,于是只要枚举切割方法就行了,区间就划分成更小的区间了.O(n^3) 四边形不等式尚待学习 #inclu ...

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

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

  5. uva 10003 Cutting Sticks (区间dp)

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

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

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

  7. uva 10003 Cutting Sticks(区间DP)

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

  8. UVA 10003 Cutting Sticks(区间dp)

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

  9. UVA 10003 Cutting Sticks

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

随机推荐

  1. 小程序本地存储之wx.getStorageSync

    这个主要可以解决微信小程序的记录缓存,入输入框的搜索历史记录 直接上代码 setsearchMsg:function(){ var that=this if (this.data.inputValue ...

  2. 在IDEA中如何使用tomcat部署项目

    1.首先,你得先建个Java 项目,然后next 2.新建完项目后,然后右击项目,选择“Add Framework Support...” 3.将Web Application 前的框勾选起来,然后点 ...

  3. 「CF55D」Beautiful numbers

    传送门 Luogu 解题思路 毒瘤数位DP,发现一个前缀我们只需要记录它对 \(\operatorname{lcm}(1,2,3,\cdots,9)=2520\) 取模的值即可,所以我们在 DP 时记 ...

  4. Python2 和 Python3 编码问题

    基本存储单元 位(bit, b):二进制数中的一个数位,可以是0或者1,是计算机中数据的最小单位. 字节(Byte,B):计算机中数据的基本单位,每8位组成一个字节. 1B = 8b 各种信息在计算机 ...

  5. Time Series_1_BRKA Case

    Berkshire Hathaway (The most expensive stock ever in the world) 1.1 Download data require(quantmod) ...

  6. 扩展新函数给window

    page.exposeFunction(name, puppeteerFunction) name <string> Name of the function on the window ...

  7. 2018--Linux面试题

    1.企业场景面试题:buffer与Cache的区别. 2.企业场景面试题:redhat与CentOS的区别. 3.企业场景面试题:  描述RAID 0 1 5 10的特点. 4.企业场景面试题:32位 ...

  8. greenplum 存储过程 函数

    参考:https://docs.pivotal.io/search?q=function

  9. layerui上传文件

    参考: http://www.layui.com/doc/modules/upload.html <1> 文件上传(以下函数必须要在js文件加载时执行) upload.render({ e ...

  10. alsa-utils 的使用

     ref : https://blog.csdn.net/outstanding_yzq/article/details/8126350 一.alsa-utils介绍 ALSA是kernel中的一个声 ...