uva10003 - Cutting Sticks(简单动规)
/* * Author: Bingo * Created Time: 2015/2/13 18:33:03 * File Name: uva10003.cpp */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <time.h> using namespace std; const int maxint = -1u>>1; int l,n; int a[1050],d[50]; int f[55][55]; int main() { while (cin>>l&&l){ cin>>n; d[0]=0; for (int i=1;i<=n;i++) { cin>>d[i]; } d[n+1]=l; n=n+2; memset(f,0,sizeof(f)); int len; for (int i=0;i<n-1;i++) f[i][i+1]=d[i+1]-d[i]; for (len=2;len<n;len++) for (int i=0;i<n-len;i++){ int &t=f[i][i+len]; t=100000; for (int k=i+1;k<i+len;k++) { t=min(t,f[i][k]+f[k][i+len]+d[i+len]-d[i]); //printf("%d %d %d\n",d[i],d[i+len],t); } } cout<<"The minimum cutting is "<<f[0][n-1]-l<<"."<<endl; } return 0; }
acm我要坚持下去,再苦也要坚持下去