标签:des c style class blog code
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11938 | Accepted: 4207 |
Description
Input
Output
Sample Input
3 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 3
Sample Output
1.633
Source
经推倒得出:n个快的平方和最小时候平方差最小
记忆化dp
#include <iostream> #include <cstring> #include <cmath> #include <algorithm> #include <cstdlib> #include <cstdio> #define N 9 #define M 16 #define INF 0x7ffffff using namespace std; int dp[N][N][N][N][M]; bool status[N][N][N][N][M]; int a[N][N],sum[N][N][N][N]; int main() { //freopen("data.txt","r",stdin); int dfs(int x1,int y1,int x2,int y2,int n); int n; while(scanf("%d",&n)!=EOF) { int ss = 0; for(int i=1;i<=8;i++) { for(int j=1;j<=8;j++) { scanf("%d",&a[i][j]); ss+=a[i][j]; } } for(int i=1;i<=8;i++) { for(int j=1;j<=8;j++) { for(int u=i;u<=8;u++) { for(int v=j;v<=8;v++) { int s = 0; for(int w=i;w<=u;w++) { for(int t=j;t<=v;t++) { s+=a[w][t]; } } sum[i][j][u][v] = s; } } } } memset(status,false,sizeof(status)); int ans = dfs(1,1,8,8,n); double ave = (double)ss/n; double res = (double)ans-2*ss*ave+ave*ave*n; printf("%.3lf\n",sqrt(res/n)); } return 0; } int dfs(int x1,int y1,int x2,int y2,int n) { if(status[x1][y1][x2][y2][n]) { return dp[x1][y1][x2][y2][n]; } if(x1==x2&&y1==y2&&n>1) { return INF; } if(n==1) { return sum[x1][y1][x2][y2]*sum[x1][y1][x2][y2]; } int ans =INF; for(int i=x1;i<=x2-1;i++) { int s1 = dfs(x1,y1,i,y2,n-1); if(s1!=INF) { s1+=sum[i+1][y1][x2][y2]*sum[i+1][y1][x2][y2]; } int s2 = dfs(i+1,y1,x2,y2,n-1); if(s2!=INF) { s2+=sum[x1][y1][i][y2]*sum[x1][y1][i][y2]; } ans = min(ans,s1); ans = min(ans,s2); } for(int i=y1;i<=y2-1;i++) { int s1 = dfs(x1,y1,x2,i,n-1); if(s1!=INF) { s1+=sum[x1][i+1][x2][y2]*sum[x1][i+1][x2][y2]; } int s2 = dfs(x1,i+1,x2,y2,n-1); if(s2!=INF) { s2+=sum[x1][y1][x2][i]*sum[x1][y1][x2][i]; } ans = min(ans,s1); ans = min(ans,s2); } status[x1][y1][x2][y2][n] = true; dp[x1][y1][x2][y2][n] = ans; return dp[x1][y1][x2][y2][n]; }
标签:des c style class blog code
原文地址:http://blog.csdn.net/acm2272902662/article/details/27685791