Poj1390-dp squares

Source: Internet
Author: User

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 ());}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.