UVA 10003 Cutting sticks interval dp//classic interval dp//dp (I,J) represents the minimum cost required to cut a small stick i-j//The state is transferred to DP (I,J) = MIN{DP (i,k) + DP (K,J) + a[j]-a[i] )//Where k>i && k<j//a[j]-a[i] is the cost of the first cut//a[0] = 0,a[n+1] = l;//When the DP array is initialized DP[I][I+1] has a value of 0, which means that//each segment is already cut , do not need to cut, other//are INF, using memory search, very simple over//////just started, I thought dp[i][i+1]=l, handed a pitch, WA//And then carefully thought about the state, found the state (i,i+1) itself indicates that there are no cutting points in the middle, and the direct//assignment is 0. And then it's over. The problem has been learned: the condition of initialization is very important. Hey, keep practicing. #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat > #include <climits> #include <cmath> #include <complex> #include <cstdio> #include < cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <vector> #define CEIL (A, B) (((a) + (b)-1)/(c)) #define Endl ' \ n ' #define GCD __gcd#define HIGHBIt (x) (1ull<< (63-__builtin_clzll (x))) #define Popcount __builtin_popcountlltypedef Long Long ll;using namespace std;const int MOD = 1000000007;const long Double PI = ACOs ( -1.L); Template<class t> inline T LCM (const t& A, cons T t& b) {return A/GCD (A, b) *b;} Template<class t> Inline T lowbit (const t& x) {return x&-x;} Template<class t> inline T maximize (t& A, const t& b) {return a=a<b?b:a;} Template<class t> inline T Minimize (t& A, const t& b) {return a=a<b?a:b;} const int MAXN = 1008;int a[maxn];int n,l;int d[maxn][maxn];const int inf = 0x3f3f3f3f;void init () {scanf ("%d", &n); for (int i=1;i<=n;i++) scanf ("%d", &a[i]); for (int. i=0;i<=n+1;i++) for (int j=0;j<=n+1;j++) d[i][j] = Inf;for ( int i=0;i<n+1;i++) d[i][i+1] = 0;a[0] = 0;a[n+1] = l;} int dp (int i,int j) {if (D[i][j]!=inf) return d[i][j];int& ans = d[i][j];for (int k=i+1;k<j;k++) ans = min (ANS,DP (i,k) +DP (K,J) +a[j]-a[i]); return ans;} void Solve () {PRINTF ("The minimum cutting is%d.\n", DP (0,N+1));} int main () {//freopen ("G:\\code\\1.txt", "R", stdin), while (scanf ("%d", &l)!=eof) {if (l==0) break;init (); Solve ();} return 0;}
UVA 10003 Cutting sticks Simple interval DP