标签:style class blog c code java
/* * @author Panoss */ #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> #include<ctime> #include<stack> #include<queue> #include<list> using namespace std; #define DBG 1 #define fori(i,a,b) for(int i = (a); i < (b); i++) #define forie(i,a,b) for(int i = (a); i <= (b); i++) #define ford(i,a,b) for(int i = (a); i > (b); i--) #define forde(i,a,b) for(int i = (a); i >= (b); i--) #define forls(i,a,b,n) for(int i = (a); i != (b); i = n[i]) #define mset(a,v) memset(a, v, sizeof(a)) #define mcpy(a,b) memcpy(a, b, sizeof(a)) #define dout DBG && cerr << __LINE__ << " >>| " #define checkv(x) dout << #x"=" << (x) << " | "<<endl #define checka(array,a,b) if(DBG) { \ dout<<#array"[] | " << endl; forie(i,a,b) cerr <<"["<<i<<"]="<<array[i]<<" |"<<((i-(a)+1)%5?" ":"\n"); if(((b)-(a)+1)%5) cerr<<endl; } #define redata(T, x) T x; cin >> x #define MIN_LD -2147483648 #define MAX_LD 2147483647 #define MIN_LLD -9223372036854775808 #define MAX_LLD 9223372036854775807 #define MAX_INF 18446744073709551615 inline int reint() { int d; scanf("%d",&d); return d; } inline long relong() { long l; scanf("%ld",&l); return l; } inline char rechar() { scanf(" "); return getchar(); } inline double redouble() { double d; scanf("%lf", &d); return d; } inline string restring() { string s; cin>>s; return s; } int f[105][105]; /* f[i][j] 表示从第i堆到第j堆石子合并的最小总得分 考虑最后一次合并,即可推得状态转移方程 f[i][j] =min( f[i][k] + f[k+1][j] + a[i] + a[i+1] + ... + a[j]) 其中 i<k<j a[i]表示第i堆石子 */ int a[105],n; void read() { forie(i,1,n) cin>>a[i]; } int sum(int l,int r) { int sum = 0; forie(i,l,r) sum +=a[i]; return sum; } int DP() { mset(f,-1); forie(i,1,n) //初始化单堆得分为0 f[i][i] = 0; forie(i,1,n-1) //相邻两堆直接加 f[i][i+1] = a[i] + a[i+1]; forie(length,3,n) { forie(i,1,n-length+1) { int j = i + length - 1; f[i][j] = f[i+1][j] + sum(i,j); fori(k,i+1,j) f[i][j] = min(f[i][k]+f[k+1][j]+sum(i,j),f[i][j]); } } return f[1][n]; } int main() { //freopen("data.txt","r",stdin); while(cin>>n,n) { read(); int min_ans = DP(); fori(i,1,n) //n-1次求解 { int temp = a[n]; forde(j,n,1) //后移 a[j] = a[j-1]; a[1] = temp; min_ans = min(min_ans,DP()); } cout << min_ans << endl; } return 0; }
标签:style class blog c code java
原文地址:http://www.cnblogs.com/Panoss/p/3741945.html