UVA - 10003 Cutting Sticks(切木棍)(dp)
题意:有一根长度为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)的更多相关文章
- UVA 10003 Cutting Sticks 切木棍 dp
题意:把一根木棍按给定的n个点切下去,每次切的花费为切的那段木棍的长度,求最小花费. 这题出在dp入门这边,但是我看完题后有强烈的既是感,这不是以前做过的石子合并的题目变形吗? 题目其实就是把n+1根 ...
- uva 10003 Cutting Sticks 【区间dp】
题目:uva 10003 Cutting Sticks 题意:给出一根长度 l 的木棍,要截断从某些点,然后截断的花费是当前木棍的长度,求总的最小花费? 分析:典型的区间dp,事实上和石子归并是一样的 ...
- UVA 10003 cuting sticks 切木棍 (区间dp)
区间dp,切割dp[i][j]的花费和切法无关(无后效性) dp[i][j]表示区间i,j的花费,于是只要枚举切割方法就行了,区间就划分成更小的区间了.O(n^3) 四边形不等式尚待学习 #inclu ...
- 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+记忆化搜索
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
题意:在给出的n个结点处切断木棍,并且在切断木棍时木棍有多长就花费多长的代价,将所有结点切断,并且使代价最小. 思路:设DP[i][j]为,从i,j点切开的木材,完成切割需要的cost,显然对于所有D ...
随机推荐
- flutter JSON序列化出现冲突
[SEVERE] Conflicting outputs were detected and the build is unable to prompt for permission to remov ...
- eclipse js文件无法保存错误
错误信息如下 Save Failedjdk.nashorn.internal.runtime.ECMAException.getEcmaError()Ljava/lang/Object; 网上多番查找 ...
- android:showAsAction
在res/layout/menu文件夹下,放置login.xml: <menu xmlns:android="http://schemas.android.com/apk/res/an ...
- windows下用libevent 开发一个echo服务
#include <stdio.h> #include <string.h> #include <errno.h> #include <iostream> ...
- 图解Mybatis框架原理及使用
1.前言 努力学习完ssm框架之后,终于也成功的把三大框架的使用以及配置文件细节忘得一干二净.为了努力捡起来以及方便今后的复习,决定写一篇博客记录一下. 本博客的所有分析都是在持久层接口以及接口中的方 ...
- Day9 - K - Yue Fei's Battle HDU - 5136
Yue Fei is one of the most famous military general in Chinese history.He led Southern Song army in t ...
- 记一次海洋cms任意代码执行漏洞拿shell(url一句话)
实验环境:海洋CMS6.54(后续版本已该洞已补) 1.后台登录尝试 这个站点是个测试站,站里没什么数据. 进入admin.php,是带验证码的后台登录系统,没有验证码的可以用bp爆破.有验证码的也有 ...
- SciPy 线性代数
章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...
- PLCsim 软件模拟OB86故障
用上一节 组态DP主站与标准从站的方法 组态了网络 实现了 将profibus –dp 标准从站 ET200M 下 输入地址为IW2 接口的状态 读取到 主机 DP-315-2DP 的QW0 变量以来 ...
- 2-10 就业课(2.0)-oozie:7、job任务的串联
4.4.oozie的任务串联 在实际工作当中,肯定会存在多个任务需要执行,并且存在上一个任务的输出结果作为下一个任务的输入数据这样的情况,所以我们需要在workflow.xml配置文件当中配置多个ac ...