# 【BZOJ1048】分割矩阵（记忆化搜索，动态规划）

## 题解

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
double sqr(double x){return x*x;}
int A,B,n;
int g[11][11];
double f[11][11][11][11][11],avg;
double dfs(int x1,int y1,int x2,int y2,int n)
{
if(n>(x2-x1+1)*(y2-y1+1))return 1e9;
if(f[x1][y1][x2][y2][n]<1e9)return f[x1][y1][x2][y2][n];
double ret=1e9;
for(int a=1;a<n;++a)
{
for(int i=x1;i<x2;++i)
ret=min(ret,dfs(x1,y1,i,y2,a)+dfs(i+1,y1,x2,y2,n-a));
for(int i=y1;i<y2;++i)
ret=min(ret,dfs(x1,y1,x2,i,a)+dfs(x1,i+1,x2,y2,n-a));
}
return f[x1][y1][x2][y2][n]=ret;
}
int main()
{
for(int i=1;i<=A;++i)
for(int i=1;i<=A;++i)
for(int j=1;j<=B;++j)g[i][j]+=g[i-1][j]+g[i][j-1]-g[i-1][j-1];
avg/=n;memset(f,127,sizeof(f));
for(int i=1;i<=A;++i)
for(int j=1;j<=B;++j)
for(int k=i;k<=A;++k)
for(int l=j;l<=B;++l)
f[i][j][k][l][1]=sqr(g[k][l]-g[i-1][l]-g[k][j-1]+g[i-1][j-1]-avg);
printf("%.2lf\n",sqrt(dfs(1,1,A,B,n)/n));
return 0;
}


posted @ 2018-08-09 20:28  小蒟蒻yyb  阅读(306)  评论(0编辑  收藏  举报