【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的更多相关文章

  1. 【Uva 307】Sticks

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

  2. 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵

    偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...

  3. 【贪心+中位数】【UVa 11300】 分金币

    (解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...

  4. 【UVa 10881】Piotr's Ants

    Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: ...

  5. 【UVa 116】Unidirectional TSP

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  6. 【UVa 1347】Tour

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. 【UVA 437】The Tower of Babylon(记忆化搜索写法)

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  8. 【uva 1025】A Spy in the Metro

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  9. 【POJ 2311】 Cutting Game

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

随机推荐

  1. lasso数学解释

    lasso:是L1正则化(绝对值) 注:坐标下降法即前向逐步线性回归 lasso算法:常用于特征选择 最小角算法,由于时间有限没有去好好研究(其实是有点复杂,尴尬)

  2. SpringAOP中的注解配置

    使用注解实现SpringAOP的功能: 例子: //表示这是被注入Spring容器中的 @Component //表示这是个切面类 @Aspect public class AnnotationHan ...

  3. 21-4indexOf

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. markdown 表情包大法

    前段时间偶然发现了markdown竟然可以插入表情,而且竟然如此的简单 表情包网站 (有可能是官网):点击跳转 这些东西真的是有点意思啊,容我举个栗子

  5. IDEA快捷键(收集自网络后整理)

    快捷键 说明 CTRL+B 快速打开光标处的类或方法 CTRL+C 拷贝 CTRL+D 复制当前行到下一行 CTRL+E 最近打开的文件 CTRL+F 当前文件查找特定内容 CTRL+G 定位行 CT ...

  6. leetcood学习笔记-79-单词搜索

    题目描述: 方法一;回溯 class Solution: def exist(self, board: List[List[str]], word: str) -> bool: max_x,ma ...

  7. [JZOJ3337] 【NOI2013模拟】wyl8899的TLE

    题目 题目大意 给你两个字符串\(A\)和\(B\),可以修改\(A\)中的一个字符,求修改后最长的\(A\)的前缀,使它是\(B\)的子串. 思考历程 看到这道题之后,第一眼想到的就是后缀自动机! ...

  8. retired!

    退役啦!估计不会再更新了,终于在大四拿到了icpc,ccpc,省赛,邀请赛金,也算是圆满了!

  9. Myeclipse从外部导入项目时,jsp和html页面中所有的onclick="return xx()"位置均出现cannot return from outside function() or method()错误

  10. 5020: [THUWC 2017]在美妙的数学王国中畅游

    传送门 当年听llj讲的时候觉得这简直是个不可做的神题. 现在看来并不是很神,可能是我已经被剧透了的缘故... 一开始以为是函数套函数,懵蔽了好久,结果只是求和 被剧透了泰勒展开就比较水了..只要你不 ...