【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 ...
随机推荐
- CentOS7 相关配置
nginx 1.在线安装nginx yum install nginx 2.启动nginx服务 systemctl start nginx 3.防火墙设置 打开http防火墙:firewall-cmd ...
- 20140402 cmake编译错误原因 同时装了vs2010和vs2012
1.cmake编译错误原因 在用cmake编译opencv出现的错误 The CXX compiler identification is MSVC 16.0.30319.1 The C compil ...
- IK 用java 代码实现分词
需要导入IK 对应的jar 包 IKAnalyzer2012.jar lucene-core-4.10.jar public static void main(String[] args) throw ...
- 编译Solr4.72 源码没有成功
最近需要用到solr,查询Hbase里面的数据,编译Solr的时候遇到了点问题: 下了solr的源码后需要用ant自己编译: 源码下载地址:https://svn.apache.org/repos/a ...
- jpa现有接口方法说明 (转https://www.cnblogs.com/rulian/p/6557471.html)
一.接口方法整理速查 下表针对于简单查询,即JpaRepository接口(继承了CrudRepository接口.PagingAndSortingRepository接口)中的可访问方法进行整理.( ...
- 可读性 vs 效率
哪个重要. 应用层代码来说,实际上说任意一个重要都不为过, 但是到了内核里面之后,哪个重要. 肯定是效率阿,内核跑得慢,上面还有得玩么.
- 现代软件工程HW2:结对编程-生成五则运算式-Core10组 [PB16110698+PB16120162]
作业具体要求点 这里 Core组要求: 1.Calc() 这个Calc 函数接受字符串的输入(字符串里就是算术表达式,例如 “5*3.5”,“7/8 - 3/8 ”,“3 + 90 * 0.3”等等) ...
- vc面试题目
class B { public: B() { cout << "default constructor" << endl; } ~B() { cout & ...
- CSIC_716_20191111【函数对象、名称空间、作用域、global 和nonlocal】
函数名是可以被引用,传递的是函数的内存地址.函数名赋值给变量后,只需要在变量后加上括号即可调用函数. 名称空间 内置名称空间:在python解释器中提前定义完的名字 全局名称空间:if.while.f ...
- leetcode-第10周双周赛-5099验证回文字符串③
题目描述: 方法:动态规划 class Solution: def isValidPalindrome(self, s: str, k: int) -> bool: def isKPalRec( ...