化简一下均方差的式子,我们就是要最小化每一个矩形的和的平方的和。可以 O(n7) dp一下qaq
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 10010
inline char gc(){
static char buf[1<<16],*S,*T;
if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
return x*f;
}
int n,m,K,f[11][11][11][11][11],sum[11][11];
inline int dfs(int x1,int y1,int x2,int y2,int k){
int &res=f[x1][y1][x2][y2][k];
if(res!=inf) return res;
if(k==1){
res=sum[x2][y2]-sum[x2][y1-1]-sum[x1-1][y2]+sum[x1-1][y1-1];
res*=res;return res;
}
for(int x=x1;x<x2;++x)
for(int j=1;j<k;++j)
res=min(res,dfs(x1,y1,x,y2,j)+dfs(x+1,y1,x2,y2,k-j));
for(int y=y1;y<y2;++y)
for(int j=1;j<k;++j)
res=min(res,dfs(x1,y1,x2,y,j)+dfs(x1,y+1,x2,y2,k-j));
return res;
}
int main(){
// freopen("a.in","r",stdin);
n=read();m=read();K=read();memset(f,inf,sizeof(f));
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j) sum[i][j]=sum[i][j-1]+sum[i-1][j]-sum[i-1][j-1]+read();
int res=dfs(1,1,n,m,K);double ave=sum[n][m]*1.0/K;
printf("%.2lf",sqrt(res*1.0/K-ave*ave));
return 0;
}