首页 > 代码库 > 【BZOJ】1048: [HAOI2007]分割矩阵

【BZOJ】1048: [HAOI2007]分割矩阵

http://www.lydsy.com/JudgeOnline/problem.php?id=1048

给出一个a×b(a,b<=10)的矩阵,带一个<=100的权值,现在要切割n-1次变成n个矩形(n<=10),求

$$\sqrt{\frac{1}{n}\sum_{i=1}^{n}(sum[i]-\mu)}, \mu = \frac{\sum_{i=1}^{n} sum[i]}{n}, sum[i]表示矩阵的和$$

的最小值

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <queue>#include <set>#include <map>#include <sstream>using namespace std;typedef long long ll;#define pb push_back#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << (#x) << " = " << (x) << endl#define error(x) (!(x)?puts("error"):0)#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)inline int getint() { static int r, k; r=0,k=1; static char c; c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }const int N=11, oo=~0u>>2;int w[N][N], f[N][N][N][N][N], A, B, n, sum[N][N];void init() {	CC(f, -1);	read(A); read(B); read(n);	for1(i, 1, A) for1(j, 1, B) read(w[i][j]);	for1(i, 1, A) for1(j, 1, B) sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+w[i][j];}int getsum(int x1, int y1, int x2, int y2) {	return sum[x2][y2]-sum[x2][y1-1]-sum[x1-1][y2]+sum[x1-1][y1-1];}int sqr(int x) { return x*x; }int dfs(int x1, int y1, int x2, int y2, int k) {	int &now=f[x1][y1][x2][y2][k];	if(now!=-1) return now;	if(k==0) return now=sqr(getsum(x1, y1, x2, y2));	if(x1==x2 && y1==y2) return now=oo;	now=oo;	--k;	for2(i, x1, x2) for1(kk, 0, k) now=min(now, dfs(x1, y1, i, y2, kk)+dfs(i+1, y1, x2, y2, k-kk));	for2(i, y1, y2) for1(kk, 0, k) now=min(now, dfs(x1, y1, x2, i, kk)+dfs(x1, i+1, x2, y2, k-kk));	return now;}int main() {	init();	double ans=(double)dfs(1, 1, A, B, n-1)-(double)sqr(sum[A][B])/(double)n;	ans=sqrt((double)1/n)*sqrt(ans);	printf("%.2f\n", ans+1e-6);	return 0;}

  

 


 

注意到数据很小....推一下公式就爆搜吧...

【BZOJ】1048: [HAOI2007]分割矩阵