【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 ...
随机推荐
- 27. USART, Universal synchronous asynchronous receiver transmitter
27.1 USART introduction 通用同步异步接收发射机(USART)对需要NRZ异步串行数据格式行业标准的外部设备,提供了一个灵活的全双工数据交换的方法.USART使用分数波特率生成器 ...
- Victor and String HDU - 5421 双向回文树
题意: 有n种操作,开始给你一个空串,给你4中操作 1 c 在字符串的首部添加字符c 2 c 在字符串的尾部添加字符c 3 询问字符中的本质不同的回文串的个数 4 询问字符串中回文串的个数 思路 ...
- 在linux 或docker中使用 system.drawing.common
在dockerfile 中添加 FROM microsoft/dotnet:2.1-aspnetcore-runtime RUN apt-get update RUN apt-get install ...
- iOS 工程实现native 跳转指定的Flutter 页面
概要 在前一篇文章中我们提到,iOS跳转到Flutter工程指定页面时(多个),Flutter只有单例,设置setInitialRouter 无效,如下 let flutterViewControll ...
- iOS开发系列-NSFileManager
NSFileManager NSFileManager类主要对文件和目录的操作(删除.修改.移动.复制等等).
- MAP(Mean Average Precision)平均精度均值
wrong 0 2 right 1 / 2 3 right 2 / 3 4 wrong 0 5 right 3 / 5 6 wrong 0 7 wrong 0 8 wrong 0 9 right 4 ...
- File转换为MultipartFile工具类
package cn.com.utils; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileu ...
- python 封装一个取符串长度的函数
def getStrLen(str): return len(str) print(getStrLen("dsa456das4dasdas21"))
- Vim统计字符串出现次数
关键命令: :%s/pattern//gn 参数说明: % - 指明操作区间,%表示全文本:可以使用1,$或者行区间代替 s – substitute,表示替换 pattern - 要查找的字符串 / ...
- 【BZOJ2298】【luoguP2519】problem a
description 一次考试共有n个人参加,第i个人说:"有ai个人分数比我高,bi个人分数比我低."问最少有几个人没有说真话(可能有相同的分数) analysis 这题转化模 ...