Question connection
Analysis:
I have been entangled in this question for a long time and cannot write dynamic equations. After reading the human code, I will use a three-digit array DP [I, j, k], indicates the maximum value that can be obtained from the number of squares that can be eliminated from the number I to the number J.
Then there are:
DP [I] [J] [k] = DP [I] [r-1] [0] + (LEN [R] + k) ^ 2; len [R] indicates the number of consecutive identical numbers from R.
Code;
# Include <stdio. h> # include <cstring> # define max (A, B) A> B? A: B; # define n 205 bool vis [N]; int idx; int A [n], color [N], next [N], Len [N]; int DP [N] [N] [N]; int result () {// put DP [N] [N] [N] [N] here the program crashes, because the array in the function cannot open such a large value, we should keep in mind that this is a loss. Memset (DP, 0, sizeof (DP); For (INT I = 1; I <idx; I ++) {for (INT L = 1; L + I-1 <idx; l ++) {int r = L + I-1; For (int K = 0; k <= next [R]; k ++) {DP [l] [r] [k] = DP [l] [r-1] [0] + (LEN [R] + k) * (LEN [R] + k); For (Int J = R-2; j> = L; j --) if (color [J] = color [R]) DP [l] [r] [k] = max (DP [l] [r] [K], DP [l] [J] [Len [R] + k] + dp [J + 1] [r-1] [0]);} return DP [1] [idx-1] [0];} int main () {int T, TMP, N, res, CA = 1, I, j; scanf ("% d", & T); While (t --) {scanf ("% d", & N); scanf ("% d ", & A [1]); idx = res = 1; for (I = 2; I <= N; I ++) {scanf ("% d ", & A [I]); if (a [I] = A [I-1]) RES ++; else {color [idx] = A [I-1]; len [idx ++] = res; Res = 1 ;}} color [idx] = A [n]; Len [idx ++] = res; memset (VIS, 0, sizeof (VIS); for (I = idx-1; I> = 1; I --) {If (vis [I]) continue; next [I] = 0; TMP = I; for (j = I-2; j> = 1; j --) if (color [J] = color [TMP]) {next [J] = next [TMP] + Len [TMP]; TMP = J; vis [j --] = 1 ;}} printf ("case % d: % d \ n ", CA ++, result ());}}