【Uva 10003】Cutting Sticks
【Link】:
【Description】
给你一根长度为l的棍子;
然后有n个切割点;
要求在每个切割点都要切割一下;
这样,最后就能形成n+1根小棍子了;
问你怎样切割,消耗的体力最小;
认为,消耗的体力,为每次切的那根棍子的长度;
【Solution】
在开头增加一个位置0,在结尾增加一个位点L;
这样,在算长度的时候比较好算一点;
长度不再是一点一点的了,而是一段一段的;
(即i..i+1代表了一段);
设f[i][j]表示,将i..j这一段切掉需要花费的最小体力值;
则在记忆化搜索中枚举切割点;
int dfs(int l,int r){
if (l+1>=r) return 0;//这里如果l+1==r,表示最小的一段木棍
//不能再切割了;
int &ans = dp[l][r];
if (ans!=INF) return ans;
for (int i = l+1;i <= r-1)
ans = min(ans,dfs(l,i)+dfs(i,r)+len(l..r));
return ans;
}
输出dfs(1,n+2);
【NumberOf WA】
1
【Reviw】
在赋初值的时候,忘记n加过2了..
【Code】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 50;
int l,c[N+10],n;
int dp[N+10][N+10];
int dfs(int l,int r){
if (l+1 >= r) return 0;
int &ans = dp[l][r];
if (ans!=-1) return dp[l][r];
rep1(i,l+1,r-1){
if (ans==-1){
ans = dfs(l,i) + dfs(i,r) + c[r]-c[l];
}else
ans = min(ans,dfs(l,i) + dfs(i,r)+c[r]-c[l]);
}
return ans;
}
int main(){
//Open();
//Close();
while (~scanf("%d",&l) && l){
rep1(i,1,N+2)
rep1(j,1,N+2)
dp[i][j] = -1;
scanf("%d",&n);
rep1(i,2,n+1)
scanf("%d",&c[i]);
c[1] = 0,c[n+2] = l;
n+=2;
printf("The minimum cutting is %d.\n",dfs(1,n));
}
return 0;
}
【Uva 10003】Cutting Sticks的更多相关文章
- 【Uva 307】Sticks
[Link]: [Description] 给你最多n个棍子; (n< = 64) 每根棍子长度(1..50) 问你这n根棍子,可以是由多少根长度为x的棍子分割出来的; x要求最小 [Solut ...
- 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵
偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...
- 【贪心+中位数】【UVa 11300】 分金币
(解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...
- 【UVa 10881】Piotr's Ants
Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: ...
- 【UVa 116】Unidirectional TSP
[Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【UVa 1347】Tour
[Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【UVA 437】The Tower of Babylon(记忆化搜索写法)
[题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【uva 1025】A Spy in the Metro
[题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【POJ 2311】 Cutting Game
[题目链接] http://poj.org/problem?id=2311 [算法] 博弈论——SG函数 [代码] #include <algorithm> #include <bi ...
随机推荐
- 最详尽使用指南:超快上手Jupyter Notebook
最详尽使用指南:超快上手Jupyter Notebook - CSDN博客https://blog.csdn.net/DataCastle/article/details/78890469
- 了解Metasploit中的Payloads(有效载荷)
什么是payload? payload又称为攻击载荷,主要是用来建立目标机与攻击机稳定连接的,可返回shell,也可以进行程序注入等.也有人把payloads称 为shellcode. Shellco ...
- Metasploit 使用MSFconsole接口
什么是MSFconsole? 该msfconsole可能是最常用的接口使用Metasploit框架(MSF).它提供了一个“一体化”集中控制台,并允许您高效访问MSF中可用的所有选项.MSFconso ...
- MyBatis中使用RowBounds对查询结果集进行分页
MyBatis可以使用RowBounds逐页加载表数据.RowBounds对象可以使用offset和limit参数来构建.参数offset表示开始位置,而limit表示要取的记录的数目 映射文件: & ...
- 大道浮屠诀---NBU报错代码之status2
在一次windows2008R2系统上部署7.7.3备份过程中遇到了此报错 当备份catalog时,会出现status 2报错 经过一系列排查后,得出如下解决方法 1.任务进程中有大量的nbtelem ...
- StringBuilder 和 StringBuffer类
通常在涉及到StringBuilder和StringBuffer时中任何一个时,都应该想到另外一个并且在脑海中问自己是否用另外一个更加合适. 为什么这么说,请继续往下看,当然如果你已经对二者烂熟于胸自 ...
- 一个好的mvc5+ef6的学习地址
链接地址: MVC5 + EF6 入门完整教程 感谢这位博主的无私奉献 文章目录列表:http://www.cnblogs.com/miro/p/3777960.html#3673688
- C 二维数组与指针
http://c.biancheng.net/view/2022.html 1. 区分指针数组和数组指针 指针数组:存放指针的数组,如 int *pstr[5] = NULL; 数组中每个元素存放的是 ...
- python 封装一个取符串长度的函数
def getStrLen(str): return len(str) print(getStrLen("dsa456das4dasdas21"))
- 单行文本截断 text-overflow
div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }