UVA10003 【Cutting Sticks】
【分析】
设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】的更多相关文章
- 【Uva 10003】Cutting Sticks
[Link]: [Description] 给你一根长度为l的棍子; 然后有n个切割点; 要求在每个切割点都要切割一下; 这样,最后就能形成n+1根小棍子了; 问你怎样切割,消耗的体力最小; 认为,消 ...
- uva 10003 Cutting Sticks 【区间dp】
题目:uva 10003 Cutting Sticks 题意:给出一根长度 l 的木棍,要截断从某些点,然后截断的花费是当前木棍的长度,求总的最小花费? 分析:典型的区间dp,事实上和石子归并是一样的 ...
- 【POJ 1011】 Sticks
[题目链接] http://poj.org/problem?id=1011 [算法] 深搜剪枝 首先我们枚举木棍的长度i,那么就有s/i根木棍,其中s为木棍长度的总和,朴素的做法就是对每种长度进行搜索 ...
- 【Codeforces 258A】 Game With Sticks
[题目链接] http://codeforces.com/contest/451/problem/A [算法] 若n和m中的最小值是奇数,则先手胜,否则后手胜 [代码] #include<bit ...
- 【POJ 2311】 Cutting Game
[题目链接] http://poj.org/problem?id=2311 [算法] 博弈论——SG函数 [代码] #include <algorithm> #include <bi ...
- 【codeforces 794B】Cutting Carrot
[题目链接]:http://codeforces.com/contest/794/problem/B [题意] 给你一个等腰三角形; 它的底边为1; 高为h; 要求你把这个等腰三角形分成n份面积相等的 ...
- 【Uva 307】Sticks
[Link]: [Description] 给你最多n个棍子; (n< = 64) 每根棍子长度(1..50) 问你这n根棍子,可以是由多少根长度为x的棍子分割出来的; x要求最小 [Solut ...
- 【27.85%】【codeforces 743D】Chloe and pleasant prizes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 757A】Gotta Catch Em' All!
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- dns未设置 PHP Warning: file_get_contents():php_network_getaddresses: getaddrinfo failed:
php通过去访问外部网站时,出现以下提示: PHP Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed ...
- 20170706wdVBA保存图片到本地API
Private Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(0 To 7) As Byte End Type Pri ...
- 1月4日编程基础hash
早上git加星了webapp的教程 > h = Hash.new('Go Fishing') => {} // 创建一个空hash,设定"Go Fishing&qu ...
- Strip CodeForces - 487B (单调队列)
题面: Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right. Now Ale ...
- Connected Components? CodeForces - 920E (bfs)
大意:给定无向图, 求补图的连通块数 bfs模拟即可, 这里用了map存图, set维护未划分的点集, 复杂度$O(nlog^2n)$, 用链表的话可以$O(n)$ #include <iost ...
- 『Scrapy』爬取斗鱼主播头像
分析目标 爬取的是斗鱼主播头像,示范使用的URL似乎是个移动接口(下文有提到),理由是网页主页属于动态页面,爬取难度陡升,当然爬取斗鱼主播头像这么恶趣味的事也不是我的兴趣...... 目标URL如下, ...
- ccfZ字形扫描
问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 3 9 3 7 5 ...
- Apache Spark探秘:利用Intellij IDEA构建开发环境
1)准备工作 1) 安装JDK 6或者JDK 7 或者JDK8 mac 的 参看http://docs.oracle.com/javase/8/docs/technotes/guide ...
- C++技能重拾
0.虽然静态成员函数不存在this指针,但还是不能在一个class里声明同名同参的虚函数和静态成员函数. 1.vftable里一个虚函数表是一个指针 2.delete本质,调用析构函数同时释放内存Ob ...
- PHP:第三章——PHP中表达式函数和匿名函数
<?php header("Content-Type:text/html;charset=utf-8"); //表达式函数和匿名函数 /*$A=function(){ ech ...