bzoj1048 [HAOI2007]分割矩阵(记忆化搜索)

化简一下均方差的式子,我们就是要最小化每一个矩形的和的平方的和。可以 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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值