【记忆化搜索】bzoj1048 [HAOI2007]分割矩阵
标准差=√(Σ(xi-xba)2/n)=Σ(xi)2+xba*n-2*xba*sum。只需最小化每个分割出来的矩阵的平方和即可。
#include<cstdio> #include<cmath> #include<algorithm> #include<cstring> using namespace std; #define INF 2000000000 typedef double db; int mem[11][11][11][11][11],a[11][11],pre[11][11],m,n,K; int sum; db xba; int sqr(int x){return x*x;} int calc(int x1,int y1,int x2,int y2) {return pre[x2][y2]-pre[x1-1][y2]-pre[x2][y1-1]+pre[x1-1][y1-1];} int f(int x1,int y1,int x2,int y2,int K) { if(mem[x1][y1][x2][y2][K]<INF) return mem[x1][y1][x2][y2][K]; if(K==1) return mem[x1][y1][x2][y2][K]=sqr(calc(x1,y1,x2,y2)); for(int i=x1;i<x2;++i) for(int j=1;j<K;++j) if((y2-y1+1)*(i-x1+1)>=j&&(y2-y1+1)*(x2-i)>=K-j) mem[x1][y1][x2][y2][K]=min(mem[x1][y1][x2][y2][K],f(x1,y1,i,y2,j)+f(i+1,y1,x2,y2,K-j)); for(int i=y1;i<y2;++i) for(int j=1;j<K;++j) if((x2-x1+1)*(i-y1+1)>=j&&(x2-x1+1)*(y2-i)>=K-j) mem[x1][y1][x2][y2][K]=min(mem[x1][y1][x2][y2][K],f(x1,y1,x2,i,j)+f(x1,i+1,x2,y2,K-j)); return mem[x1][y1][x2][y2][K]; } int main() { scanf("%d%d%d",&n,&m,&K); for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) { scanf("%d",&a[i][j]); sum+=a[i][j]; pre[i][j]=pre[i][j-1]+a[i][j]; } xba=(db)sum/(db)K; for(int j=1;j<=m;++j) for(int i=1;i<=n;++i) pre[i][j]+=pre[i-1][j]; memset(mem,0x7f,sizeof(mem)); printf("%.2f\n",sqrt(((db)f(1,1,n,m,K)+(db)K*xba*xba-2.0*(db)sum*xba)/(db)K)); return 0; }
——The Solution By AutSky_JadeK From UESTC
转载请注明出处:http://www.cnblogs.com/autsky-jadek/