10003 - UVa Online Judge
2014-03-14 22:17
232 查看
Cutting Sticks |
that they only make one cut at a time.
It is easy to notice that different selections in the order of cutting can led to different prices. For example, consider a stick of length 10 meters that has to be cut at 2, 4 and 7 meters from one end. There are
several choices. One can be cutting first at 2, then at 4, then at 7. This leads to a price of 10 + 8 + 6 = 24 because the first stick was of 10 meters, the resulting of 8 and the last one of 6. Another choice could be cutting at 4, then at 2, then at 7. This
would lead to a price of 10 + 4 + 6 = 20, which is a better price.
Your boss trusts your computer abilities to find out the minimum cost for cutting a given stick.
Input
The input will consist of several input cases. The first line of each test case will contain a positive number l thatrepresents the length of the stick to be cut. You can assume l < 1000. The next line will contain the number n (n <
50) of cuts to be made.
The next line consists of n positive numbers ci ( 0 < ci < l) representing the places where the cuts have to be done, given in strictly
increasing order.
An input case with l = 0 will represent the end of the input.
Output
You have to print the cost of the optimal solution of the cutting problem, that is the minimum cost of cutting the given stick. Format the output as shown below.Sample Input
100 3 25 50 75 10 4 4 5 7 8 0
Sample Output
The minimum cutting is 200. The minimum cutting is 22.
//区间dp,f(i, j) = min{ f(i,k)+f(k+1,j)+c[r]-c[l-1] | l<=k<k } #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=100; const int INF=1000000; int dp ; int num ; int minNum(int a,int b) { return a<b?a:b; } int main() { int n,m; while(scanf("%d",&m)==1) { if(m==0) break; cin>>n; num[0]=0; num[n+1]=m; int i,j,k; memset(dp,0,sizeof(dp)); for(i=1;i<=n;i++) { cin>>num[i]; } for(i=2;i<=n+1;i++) { for(j=0;j<=n-i+1;j++) { int min=INF,k=i+j; for(int d=j+1;d<k;d++) { min=minNum(min,dp[j][d]+dp[d][k]); } dp[j][k]=min+num[k]-num[j]; } } cout<<"The minimum cutting is "<<dp[0][n+1]<<"."<<endl; } return 0; }
相关文章推荐
- jsp学习--JSP运行原理,九大隐式对象和JSP常用标签
- 用jdbc连接Oracle数据库
- C++浅层与深层复制构造函数
- Incorrect string value: '\xF0\x9F\x92\x90</...' for column 'xxx' at row 1
- for循环语句详解
- error while loading shared libraries: libluajit-5.1.so.2
- linux下配置ip地址四种方法(图文方法
- 嵌入式系统设计师考试完美笔记
- Head First -- 工厂方法
- 公司java项目开发规范
- AS3的SWC代码混淆方案
- PreferenceActivity:类似于setting菜单样式的activtity
- ORACLE PLSQL DEVELOPER 中显示科学计数法
- 黑马程序员-第十九天( IO(Input Output)流)
- RichEdit 控件:语法高亮显示
- TC win tc C标准 C ANSI 获取系统时间的方法
- C语言中break和continue的区别
- Windows Server 2012 R2 文件服务器安装与配置07 之文件服务器高可用配置DFS
- C#中数据传输转换之XML
- SqlDependency学习笔记