码迷,mamicode.com
首页 > 其他好文 > 详细

1048: [HAOI2007]分割矩阵——记忆化搜索

时间:2015-05-28 21:37:35      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

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

TML了一发

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define clr(a,b) memset(a,b,sizeof(a))
const double INF = 0x3f3f3f3f;
using namespace std;
int a[11][11];
int sum[11][11],sumx[11][11],sumy[11][11];
double dp[11][11][11][11][11];
int n,m,k;
double ave;
double solve(int x1,int y1,int x2,int y2,int k)
{
    double &ans=dp[x1][y1][x2][y2][k];
    if(ans>=0.0) return ans;
    if(k==1){
        int t=sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1];
        return ans=(t-ave)*(t-ave);
    }
    ans=INF;
    for(int i=y1;i<y2;++i){
        for(int j=1;j<k;++j){
            ans=min(ans,solve(x1,y1,x2,i,j)+solve(x1,i+1,x2,y2,k-j));
        }
    }
    for(int i=x1;i<x2;++i){
        for(int j=1;j<k;++j){
            ans=min(ans,solve(x1,y1,i,y2,j)+solve(i+1,y1,x2,y2,k-j));
        }
    }
    return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // ONLINE_JUDGE
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=n;++i){
        for(int j=1;j<=m;++j){
            scanf("%d",&a[i][j]);
            sumx[i][j]=sumx[i][j-1]+a[i][j];
            sumy[j][i]=sumy[j][i-1]+a[i][j];
        }
    }
    for(int i=1;i<=n;++i){
        for(int j=1;j<=m;++j){
            sum[i][j]=sum[i-1][j-1]+sumx[i][j-1]+sumy[j][i-1]+a[i][j];
        }
    }
    ave=sum[n][m]*1.0/k;
    clr(dp,0xff);
    double ans=solve(1,1,n,m,k);
    printf("%.2f\n",sqrt(ans/k));
    return 0;
}

1048: [HAOI2007]分割矩阵——记忆化搜索

标签:

原文地址:http://blog.csdn.net/u014141559/article/details/46127529

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!