poj 1191
这题好像有点卡精度啊。。我最后用c++交,ac了
# include <cstdio> # include <cstring> # include <cmath> # include <iostream> # define inf 0x3fffffff using namespace std; int map[8][8]; double ave,d[20][10][10][10][10],sum[10][10]; double f(int i,int j,int m,int n) { double t = sum[j+1][n+1] - sum[j+1][m] - sum[i][n+1] + sum[i][m]; return (t - ave)*(t- ave); } double dp(int k,int i,int j,int m,int n) { double& res = d[k][i][j][m][n]; if (k == 1) return f(i,j,m,n); if (res>1e-6) return res; int c; res = inf; for (c=i;c<j;++c) res = min(res,min(dp(k-1,i,c,m,n)+f(c+1,j,m,n),dp(k-1,c+1,j,m,n)+f(i,c,m,n))); for (c=m;c<n;++c) res = min(res,min(dp(k-1,i,j,m,c)+f(i,j,c+1,n),dp(k-1,i,j,c+1,n)+f(i,j,m,c))); return res; } int main() { int n,i,j,k,m,t,s=0; scanf("%d",&n); for (i=0;i<8;++i) for (j=0;j<8;++j) { scanf("%d",&map[i][j]); s += map[i][j]; } for (i=1;i<=8;++i) for (j=1;j<=8;++j) sum[i][j] = sum[i-1][j]+sum[i][j-1]+map[i-1][j-1]-sum[i-1][j-1]; ave = s/(n*1.0); memset(d,-1,sizeof(d)); double ans = sqrt(dp(n,0,7,0,7)/n); printf("%.3lf\n",ans); return 0; }