UVA 10003 —— 区间DP
2013-11-07 11:41
344 查看
10003 - Cutting Sticks
Time limit: 3.000 secondsCutting Sticks |
requires 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.
Miguel Revilla
2000-08-21
题意是给你一个n长度的木棒,然后从m个位置进行分割,每次每个的花费为当前木棒的长度,让你安排这些分割的次序使之花费最小。
思路就是枚举区间中的一个值进行区间DP。
/* ID: xinming2 PROG: stall4 LANG: C++ */ #include <cstdio> #include <cmath> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include <string> #include <stack> #include <cctype> #include <vector> #include <queue> #include <set> #include <utility> #include <cassert> using namespace std; ///#define Online_Judge #define outstars cout << "***********************" << endl; #define clr(a,b) memset(a,b,sizeof(a)) #define lson l , mid , rt << 1 #define rson mid + 1 , r , rt << 1 | 1 #define mk make_pair #define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++) #define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++) #define REP(i , x , n) for(int i = (x) ; i > (n) ; i--) #define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--) const int MAXN = 15 + 50; const int sigma_size = 26; const long long LLMAX = 0x7fffffffffffffffLL; const long long LLMIN = 0x8000000000000000LL; const int INF = 0x7fffffff; const int IMIN = 0x80000000; #define eps 1e-8 const int mod = (int)1e9 + 7; typedef long long LL; const LL MOD = 1000000007LL; const double PI = acos(-1.0); typedef pair<int , int> pi; #define Bug(s) cout << "s = " << s << endl; ///#pragma comment(linker, "/STACK:102400000,102400000") int dp[MAXN][MAXN] ;///dp[i][j]表示从i到j的最小花费 int a[MAXN] , vis[MAXN][MAXN]; int DP(int i , int j) { if(vis[i][j])return dp[i][j]; vis[i][j] = 1; if(j - i == 1)return 0; int min_i = INF; for(int k = i + 1 ; k < j ; k++) { min_i = min(min_i , DP(i , k) + DP(k , j) + a[j] - a[i]); } dp[i][j] =min_i; return min_i; } int main() { int n , m; while(~scanf("%d" , &n) , n) { clr(dp , 0); clr(vis , 0); scanf("%d" , &m); for(int i = 1 ;i <= m ; i++) { scanf("%d", &a[i]); } a[++m] = n; printf("The minimum cutting is %d.\n" , DP(0 , m)); } return 0; }
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- zoj3549 快速幂
- NWERC2010 NKOJ2178 Stock Prices
- 2011ACM福州网络预选赛B题 HDU4062 Abalone
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- ACM常用算法
- 2013 Multi-University Training Contest 1
- ACM/ICPC需要掌握的知识[转载]
- 北大—1006——Biorhythms
- ZOJ Problem Set - 1711 解题报告
- ZOJ Problem Set - 2412解题报告
- ZOJ Problem Set - 1457 解题报告