# bzoj1048 [HAOI2007]分割矩阵

### Solution

#include<cmath>
#include<queue>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

#define rep(i, a, b) for (int i = a; i <= b; i++)
#define drp(i, a, b) for (int i = a; i >= b; i--)
#define fech(i, x) for(int i = 0; i < x.size(); i++)
#define N 15

int x = 0, flag = 1; char ch = getchar();
while (ch > '9' || ch < '0') { if (ch == '-') flag = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * flag;
}
inline void write(int x) { if (x >= 10) write(x / 10); putchar(x % 10 + '0'); }

double  f[N][N][N][N][N];
int s[N][N];
double ba;

double dfs(int a, int b, int c, int d, int k) {
double tmp;
if (f[a][b][c][d][k] != -1) return f[a][b][c][d][k];
if (k == 1) {
tmp = s[c][d] + s[a - 1][b - 1] - s[c][b - 1] - s[a - 1][d];
return f[a][b][c][d][k] = (tmp - ba) * (tmp - ba);
}
int i, j; tmp = 1e9;
rep(i, a, c - 1) rep(j, 1, k - 1) tmp = min(tmp, dfs(a, b, i, d, j) + dfs(i + 1, b, c, d, k - j));
rep(i, b, d - 1) rep(j, 1, k - 1) tmp = min(tmp, dfs(a, b, c, i, j) + dfs(a, i + 1, c, d, k - j));
return f[a][b][c][d][k] = tmp;
}
int main() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
rep(i, 1, n) rep(j, 1, m) s[i][j] = read() + s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
rep(a, 0, n + 1) rep(b, 0, m + 1) rep(c, 0, n + 1) rep(d, 0, m + 1) rep(t, 0, k + 1)
f[a][b][c][d][t] = -1;
ba = (double)s[n][m] / k;
printf("%.2f", sqrt(dfs(1, 1, n, m, k) / k));
return 0;
}

posted @ 2018-02-05 09:16  aziint  阅读(129)  评论(0编辑  收藏  举报