POJ1991 NOI1999棋盘分割

棋盘分割
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 15581   Accepted: 5534

Description

将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的部分继续如此分割,这样割了(n-1)次后,连同最后剩下的矩形棋盘共有n块矩形棋盘。(每次切割都只能沿着棋盘格子的边进行)

原棋盘上每一格有一个分值,一块矩形棋盘的总分为其所含各格分值之和。现在需要把棋盘按上述规则分割成n块矩形棋盘,并使各矩形棋盘总分的均方差最小。
均方差,其中平均值,xi为第i块矩形棋盘的总分。
请编程对给出的棋盘及n,求出O'的最小值。

Input

第1行为一个整数n(1 < n < 15)。
第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

Source

 
【题解】
先推一发式子,小学数学
然后DP
dp[i][x1][y1][x2][y2]表示矩形(x1,y1),(x2,y2)分成i块的平方和
普及组转移
有个大坑:
printf保留小数的时候自动四舍五入
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cmath>
 6 #define min(a, b) ((a) < (b) ? (a) : (b))
 7 #define dist(x1, y1, x2, y2) (g[(x2)][(y2)] - g[((x1) - 1)][(y2)] - g[(x2)][((y1) - 1)] + g[((x1) - 1)][((y1) - 1)])
 8 
 9 inline void read(long long &x)
10 {
11     x = 0;char ch = getchar(), c = ch;
12     while(ch < '0' || ch > '9')c = ch, ch = getchar();
13     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
14     if(c == '-')x = -x; 
15 }
16 
17 const long long MAXN = 15 + 5;
18 
19 long long n, g[10][10], dp[MAXN][10][10][10][10], sum; 
20 
21 int main()
22 {
23     read(n);
24     for(register long long i = 1;i <= 8;++ i)
25         for(register long long j = 1;j <= 8;++ j)
26         {
27             read(g[i][j]);
28             sum += g[i][j];
29             g[i][j] = g[i - 1][j] + g[i][j - 1] - g[i - 1][j - 1] + g[i][j];
30         }
31     memset(dp, 0x3f, sizeof(dp));
32     //dp[i][x1][y1][x2][y2]表示把(x1,y1)(x2,y2)矩形切割成i块的最小平方和
33     for(register long long i = 1;i <= n;++ i)
34         for(register long long x1 = 1;x1 <= 8;++ x1)
35             for(register long long y1 = 1;y1 <= 8;++ y1)
36                 for(register long long x2 = 1;x2 <= 8;++ x2)
37                     for(register long long y2 = 1;y2 <= 8;++ y2)
38                     {
39                         if(i == 1)
40                         {
41                             dp[i][x1][y1][x2][y2] = dist(x1, y1, x2, y2)*dist(x1, y1, x2, y2); 
42                             continue;
43                         }
44                         for(register long long a = x1;a < x2;++ a)
45                         {
46                             dp[i][x1][y1][x2][y2] = min(dp[i][x1][y1][x2][y2], dp[i - 1][x1][y1][a][y2] + dist(a + 1, y1, x2, y2)*dist(a + 1, y1, x2, y2));
47                             dp[i][x1][y1][x2][y2] = min(dp[i][x1][y1][x2][y2], dp[i - 1][a + 1][y1][x2][y2] + dist(x1, y1, a, y2)*dist(x1, y1, a, y2));
48                         }
49                         for(register long long a = y1;a < y2;++ a)
50                         {
51                             dp[i][x1][y1][x2][y2] = min(dp[i][x1][y1][x2][y2], dp[i - 1][x1][y1][x2][a] + dist(x1, a + 1, x2, y2)*dist(x1, a + 1, x2, y2));
52                             dp[i][x1][y1][x2][y2] = min(dp[i][x1][y1][x2][y2], dp[i - 1][x1][a + 1][x2][y2] + dist(x1, y1, x2, a)*dist(x1, y1, x2, a));
53                         }
54                     }
55     printf("%.3lf", (double)sqrt(((double)dp[n][1][1][8][8]*1.0/n) - ((double)sum*1.0/n) * ((double)sum*1.0/n)));
56     return 0;
57 }
POJ1991

 

 
posted @ 2017-09-07 11:48  嘒彼小星  阅读(304)  评论(0编辑  收藏  举报