【分析】

设d(i,j)为切割小木棍i~j的最优费用,则d(i,j)=min{d(i,k)+d(k,j)|i<k<j}+a[j]-a[i],其
中最后一项a[j]-a[i]代表第一刀的费用。切完之后,小木棍变成i~k和k~j两部分,状态转
移方程由此可得。把切割点编号为1~n,左边界编号为0,右边界编号为n+1,则答案
为d(0,n+1)。
状态有O(n2)个,每个状态的决策有O(n)个,时间复杂度为O(n3)。

【实现】

递推版本要枚举区间长,我个人认为比较僵硬,于是我写的是记忆化搜索。
附上AC代码。(我的命名均是有意义的,结合分析应该能看懂,没有注释请见谅)。

#include<bits/stdc++.h>
using namespace std;
template<class T> inline T read(T&x){
    T data=0;
int w=1;
    char ch=getchar();
    while(ch!='-'&&!isdigit(ch))
        ch=getchar();
    if(ch=='-')
        w=-1,ch=getchar();
    while(isdigit(ch))
        data=10*data+ch-'0',ch=getchar();
    return x=data*w;
}
/*
100
3
25 50 75
10
4
4 5 7 8
0
*/
const int maxn=55;
int c[maxn];
int d[maxn][maxn];

int dp(int l,int r){
    if(d[l][r]!=-1)
        return d[l][r];
    if(r-l==1)
        return d[l][r]=0;
    int minans=200000;
    for(int k=l+1;k<=r-1;++k)
        if(dp(l,k)+dp(k,r)<minans)
            minans=dp(l,k)+dp(k,r);
    return d[l][r]=minans+c[r]-c[l];
}

int main(){
    int l,n;
    while(read(l)){
        read(n);
        c[0]=0,c[n+1]=l;
        for(int i=1;i<=n;++i)
            read(c[i]);
        memset(d,-1,sizeof(d));
        dp(0,n+1);
        printf("The minimum cutting is %d.\n",d[0][n+1]);
    }
    return 0;
}

UVA10003 【Cutting Sticks】的更多相关文章

  1. 【Uva 10003】Cutting Sticks

    [Link]: [Description] 给你一根长度为l的棍子; 然后有n个切割点; 要求在每个切割点都要切割一下; 这样,最后就能形成n+1根小棍子了; 问你怎样切割,消耗的体力最小; 认为,消 ...

  2. uva 10003 Cutting Sticks 【区间dp】

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

  3. 【POJ 1011】 Sticks

    [题目链接] http://poj.org/problem?id=1011 [算法] 深搜剪枝 首先我们枚举木棍的长度i,那么就有s/i根木棍,其中s为木棍长度的总和,朴素的做法就是对每种长度进行搜索 ...

  4. 【Codeforces 258A】 Game With Sticks

    [题目链接] http://codeforces.com/contest/451/problem/A [算法] 若n和m中的最小值是奇数,则先手胜,否则后手胜 [代码] #include<bit ...

  5. 【POJ 2311】 Cutting Game

    [题目链接] http://poj.org/problem?id=2311 [算法] 博弈论——SG函数 [代码] #include <algorithm> #include <bi ...

  6. 【codeforces 794B】Cutting Carrot

    [题目链接]:http://codeforces.com/contest/794/problem/B [题意] 给你一个等腰三角形; 它的底边为1; 高为h; 要求你把这个等腰三角形分成n份面积相等的 ...

  7. 【Uva 307】Sticks

    [Link]: [Description] 给你最多n个棍子; (n< = 64) 每根棍子长度(1..50) 问你这n根棍子,可以是由多少根长度为x的棍子分割出来的; x要求最小 [Solut ...

  8. 【27.85%】【codeforces 743D】Chloe and pleasant prizes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【codeforces 757A】Gotta Catch Em' All!

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. Failed to execute operation: No such file or directory(systemctl enable iptables.service)

    在保存Iptables配置时:systemctl enable iptables.service 出现错误: Failed to execute operation: No such file or ...

  2. memcached哈希表操作主要逻辑笔记

    以下注释的源代码都在memcached项目的assoc.c文件中 /* how many powers of 2's worth of buckets we use */ unsigned int h ...

  3. PC端,移动端分离,如何结合??

    <script type="text/javascript"> function mobile_device_detect(url) { var thisOS = na ...

  4. AND Graph CodeForces - 987F (状压)

    链接 大意:给定$m$个数, 若$x\&y=0$, 则在$x$与$y$之间连一条无向边. 求无向图的连通块个数 暴力连边显然超时的, 可以通过辅助结点优化连边, 复杂度$O(n2^n)$ #i ...

  5. 『Scipy』常用方法记录

    优化器使用教程 J = lambda wb: self.get_cost_grad(wb, X, Y_one_hot) theta = self.wb_init(X,Y_one_hot) result ...

  6. [LeetCode] 41. First Missing Positive ☆☆☆☆☆(第一个丢失的正数)

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  7. JVM笔记(二) 垃圾收集器(1)

    垃圾收集器 主要通过阅读<深入了解Java虚拟机>(周志明 著)和网络资源汇集而成,为本人学习JVM的笔记.同时,本文理论基于JDK 1.7版本,暂不考虑 1.8和1.9 的新特性,但可能 ...

  8. forget word out4

    1★ be 使~ 成为:   2★ bene bene   3★ bi 2,两个,双重   4★ by 在~ 旁边,副的  

  9. Error: Chunk.entry was removed. Use hasRuntime()错误解决

      Error: Chunk.entry was removed. Use hasRuntime()错误解决           执行如下命令 npm uninstall --save-dev ext ...

  10. Using Fetch to rewrite JSON

    截图如下: html代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g ...