ccf壓縮編碼(石子排序,四邊形優化)
阿新 • • 發佈:2019-01-31
#include <iostream> #include <cstdio> using namespace std; const int maxn = 1005; const int INF = 0x3f3f3f3f; int a[maxn]; int sum[maxn]; int dp[maxn][maxn]; int s[maxn][maxn]; int n; void init() { for(int i = 1;i<=n;i++) { dp[i][i] = 0; s[i][i] = i; } for(int i = 1;i<=n-1;i++) { dp[i][i+1] = sum[i+1]-sum[i-1]; s[i][i+1] = i; } } void solve() { for(int r = 2;r<=n-1;r++) { for(int i = 1;i+r<=n;i++) { int j = i+r; dp[i][j] = INF; for(int k=s[i][j-1];k<=s[i+1][j];k++) { if(dp[i][j]>dp[i][k]+dp[k+1][j]) { dp[i][j] = dp[i][k]+dp[k+1][j]; s[i][j] = k; } } dp[i][j]+=(sum[j]-sum[i-1]); } } } int main() { scanf("%d",&n); sum[0] = 0; for(int i = 1;i<=n;i++) { scanf("%d",&a[i]); sum[i] = a[i]+sum[i-1]; } init(); solve(); cout<<dp[1][n]<<endl; }