N-knife cutting board
The following figure shows the 8x8 chessboard. Each number represents the weight of the corresponding vertex of the chessboard. After n knives are cut, the minimum mean variance of the sum of each shard is
The formula for the mean variance must be simplified first:
From the above formula, the minimum mean variance is obviously the smallest of Xi ^ 2
D [k] [x1] [y1] [x2] [y2] represents the smallest sum of squares obtained by the k-knife from (x1, y1)-> (x2, y2)
Sum [I] [j] indicates the sum of values from () to (I, j ).
The answer is dp [n] [1] [1] [8] [8]/n-(sum [8] [8]/n) ^ 2.
Use S [(x1, y1), (x2, y2)] to represent the weights and
Recursive dp is used here.
State transition equation:
D [k] [x1] [y1] [x2] [y2] =
Min {transverse cut: d [k-1] + sum of squares of the remaining uncut part, longitudinal cut: d [k-1] + sum of squares of the remaining uncut part}
The optimal solution of transverse tangent is Min {d [k-1, (x1, y1), (I, y2)] + S [(I + 1, y1), (x2, y2)], d [k-1, (I + 1, y1), (x2, y2)] + S [(x1, y1), (I, y2)]} (x1 <= I <x2)
The above means: the optimal solution of cutting a knife at x = I
Likewise, it is easy to introduce the dp equation of the vertical tangent method.
# Include <stdio. h> # include <math. h> # include <string. h> # define INF 1 <29int map [9] [9], sum [9] [9]; int d [15] [9] [9] [9] [9]; inline int Min (int a, int B) {return a> B? B: a;} int s (int x1, int y1, int x2, int y2) {int temp = sum [x2] [y2]-sum [x1-1] [y2]-sum [x2] [y1-1] + sum [x1-1] [y1-1]; return temp * temp;} int dp (int k, int x1, int y1, int x2, int y2) {if (d [k] [x1] [y1] [x2] [y2]! =-1) return d [k] [x1] [y1] [x2] [y2]; if (k = 1) return s (x1, y1, x2, y2); int ans = INF, I; for (I = x1; I <x2; I ++) {ans = Min (K-1, x1, y1, I, y2) + s (I + 1, y1, x2, y2), ans); ans = Min (dp (K-1, I + 1, y1, x2, y2) + s (x1, y1, I, y2), ans) ;}for (I = y1; I <y2; I ++) {ans = Min (dp (K-1, x1, y1, x2, I) + s (x1, I + 1, x2, y2), ans); ans = Min (dp (K-1, x1, I + 1, x2, y2) + s (x1, y1, x2, I), ans );} return d [k] [x1] [y1] [x2] [y2] = ans;} int main () {int n, I, j, k; while (~ Scanf ("% d", & n) {memset (map, 0, sizeof (map); for (I = 1; I <= 8; I ++) for (j = 1; j <= 8; j ++) scanf ("% d", & map [I] [j]); memset (sum, 0, sizeof (sum); for (I = 1; I <= 8; I ++) for (j = 1; j <= 8; j ++) sum [I] [j] = sum [I-1] [j] + sum [I] [J-1]-sum [I-1] [J-1] + map [I] [j]; // get the sum array memset (d,-1, sizeof (d); int ans = dp (n,); double aver = (double) sum [8] [8]/(double) n; double last = sqrt (double) ans/(double) n-aver * aver); printf ("%. 3lf \ n ", last);} return 0 ;}