棋盘分割
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 13516 | Accepted: 4818 |
Description
将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的部分继续如此分割,这样割了(n-1)次后,连同最后剩下的矩形棋盘共有n块矩形棋盘。(每次切割都只能沿着棋盘格子的边进行)
原棋盘上每一格有一个分值,一块矩形棋盘的总分为其所含各格分值之和。现在需要把棋盘按上述规则分割成n块矩形棋盘,并使各矩形棋盘总分的均方差最小。
均方差
,其中平均值
,x
i为第i块矩形棋盘的总分。
请编程对给出的棋盘及n,求出O'的最小值。
![](https://i-blog.csdnimg.cn/blog_migrate/ff1abff721dae865e9d972aaca976624.jpeg)
原棋盘上每一格有一个分值,一块矩形棋盘的总分为其所含各格分值之和。现在需要把棋盘按上述规则分割成n块矩形棋盘,并使各矩形棋盘总分的均方差最小。
均方差
![](https://i-blog.csdnimg.cn/blog_migrate/5a2f2dbeba50c9315a715c7ddedf1f13.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f959afa86028336bd6e867b9c3c9fe46.png)
请编程对给出的棋盘及n,求出O'的最小值。
Input
第1行为一个整数n(1 < n < 15)。
第2行至第9行每行为8个小于100的非负整数,表示棋盘上相应格子的分值。每行相邻两数之间用一个空格分隔。
第2行至第9行每行为8个小于100的非负整数,表示棋盘上相应格子的分值。每行相邻两数之间用一个空格分隔。
Output
仅一个数,为O'(四舍五入精确到小数点后三位)。
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
第一道真正意义上的区间dp。
转化式子
S^2 = 1/n∑(Xi - aver_X)^2 = 1/n (n * aver_X^2 + ∑Xi^2 - 2 * aver_X ∑Xi) = 1/n∑Xi^2 - avre_X^2;
只需要求出切割得到的k个矩形 的最小的 分值和平方。
思路:设dp[k][x1][y1][x2][y2]为 以坐标[x1, y1]为左上角、坐标[x2, y2]为右下角的矩形切割k次后获得的最小分值。sum(x1, y1, x2, y2)为以坐标[x1, y1]为左上角、坐标[x2, y2]为右下角的矩形的分值和的平方。
状态转移方程:
切横行x1 <= row < x2
dp[k][x1][y1][x2][y2]
= min(dp[k-1][x1][y1][row][y2]+sum(row+1,y1,x2,y2), dp[k-1][row+1][y1][x2][y2]+sum(x1,y1,row,y2));
切纵行y1 <= cul < y2
dp[k][x1][y1][x2][y2]
= min(dp[k-1][x1][y1][x2][cul]+sum(x1,cul+1,x2,y2), dp[k-1][x1][cul+1][x2][y2]+sum(x1,y1,x2,cul));
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (200+10)
#define MAXM (50000000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
int s[9][9];
int a[9][9];
int ans[15][9][9][9][9];
int getsum(int x1, int y1, int x2, int y2)
{
int temp = s[x2][y2] - s[x2][y1-1] - s[x1-1][y2] + s[x1-1][y1-1];
return temp*temp;
}
int dp(int k, int x1, int y1, int x2, int y2)
{
if(ans[k][x1][y1][x2][y2] != -1)
return ans[k][x1][y1][x2][y2];
if(k == 0)
return getsum(x1, y1, x2, y2);
ans[k][x1][y1][x2][y2] = INF;
for(int row = x1; row < x2; row++)
ans[k][x1][y1][x2][y2] = min(ans[k][x1][y1][x2][y2], min(dp(k-1, x1, y1, row, y2)+getsum(row+1, y1, x2, y2), dp(k-1, row+1, y1, x2, y2)+getsum(x1, y1, row, y2)));
for(int cul = y1; cul < y2; cul++)
ans[k][x1][y1][x2][y2] = min(ans[k][x1][y1][x2][y2], min(dp(k-1, x1, y1, x2, cul)+getsum(x1, cul+1, x2, y2), dp(k-1, x1, cul+1, x2, y2)+getsum(x1, y1, x2, cul)));
return ans[k][x1][y1][x2][y2];
}
int main()
{
int n;
while(Ri(n) != EOF)
{
int sum = 0; CLR(s, 0);
for(int i = 1; i <= 8; i++)
for(int j = 1; j <= 8; j++)
Ri(a[i][j]), sum += a[i][j];
for(int i = 1; i <= 8; i++)
{
for(int j = 1; j <= 8; j++)
{
if(i == 1 && j == 1)
s[i][j] = a[i][j];
else if(i == 1)
s[i][j] = s[i][j-1] + a[i][j];
else if(j == 1)
s[i][j] = s[i-1][j] + a[i][j];
else
s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + a[i][j];
}
}
CLR(ans, -1);
printf("%.3lf\n", sqrt((dp(n-1, 1, 1, 8, 8)*n - sum*sum)*1.0 / n / n));
}
return 0;
}