标签:
6 3 1 1 0 1 0 1
2
大意:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAX = 600,inf = 0x3f3f3f3f; int a[MAX],b[MAX],w[MAX],dp[MAX][MAX]; int main() { int n,k; scanf("%d%d",&n,&k); for(int i = 1; i <= n;i++) scanf("%d",&a[i]); for(int i = 1; i <= n;i++) for(int j = 1; j <= k; j++) dp[i][j] = inf; memset(b,0,sizeof(b)); memset(w,0,sizeof(w)); for(int i = 1 ;i <= n;i++){ if(a[i] == 0){ w[i] = w[i-1] + 1; b[i] = b[i-1]; } else { b[i] = b[i-1] + 1; w[i] = w[i-1]; } dp[i][1] = b[i]*w[i]; } for(int i = 2; i <= k;i++){ for(int j = i; j <=n;j++){ for(int p = i-1;p < j;p++){ int t1 = w[j] - w[p]; int t2 = b[j] - b[p]; dp[j][i] = min(dp[j][i],dp[p][i-1]+t1*t2); } } } printf("%d",dp[n][k]); return 0; }
标签:
原文地址:http://www.cnblogs.com/zero-begin/p/4374605.html