hdu4283 you is the one interval DP memory search or recursion

Source: Internet
Author: User

You is the oneTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3032 Accepted Submission (s): 1352


Problem Description The TV shows such as you is the one has been very popular. In order to meet the need of boys who is still single, Tjut hold the show itself. The show is on hold at the Small Hall, so it attract a lot of boys and girls. Now there is n boys enrolling in. At the beginning, the N boys stand in a row and go to the stage one by one. However, the director suddenly knows that very boy had a value of Diaosi D, if the boy was k-th one go to the stage, the UN Happiness of him would be (k-1) *d, because he had to wait for (k-1) people. Luckily, there is a dark-in-the-Small hall, so the director can put the boy into the dark the class temporarily and let th E Boys behind he go to stage before him. For the dark-the-very narrow, the boy who first get into dark. The director wants to the order of boys by the Dark hostel, so the summary of unhappiness would be least. Can you help him?

Input The first line contains a single integer T, the number of test cases. For each case, the first line is n (0 < n <= 100)
The next n line is n integer d1-dn means the value of Diaosi of boys (0 <= Di <= 100)

Output for each test case, output the least summary of unhappiness.

Sample Input
2 512345554322


Read half a morning on the internet to basically understand the puzzle ... Good subtlety of boundary handling ... dp Dafa good


Test instructions said 1-n a dick, and then through a small black house is a stack can change their order, everyone has a cock silk value, and then if this person row K, then his unhappy value is (k-1) * D

Ask everyone about the sum of the unhappy values of the minimum is how much


First Analyze this problem, for the interval 1-n, the first person may be the first to play, may also be the nth to play, in fact, for the first person, he may be the 1-n K in the play (1 <= k <= N)

Why could he be one of the first 1-n to play, because

For example.

Original team: 1 2 3 4 5

Bottom-->2 3 4 5<--Top stage: 1

1 directly on the stage, the remaining four can be directly on, can also be advanced small black house again, this time 1 is the first one on the


Bottom-->1 2 3 4 5<--stack top stage: null

Bottom-->1 2 3<--stack Top stage: 4 5

For example, in both cases, 1 is the last to play.


and between the 1-n.

Team: 3 4 5

Bottom-->1 2<--stack top stage: null

Then let 1 2 out of the stack first.

Team: 3 4 5

Bottom--><--Stack top stage: 2 1

Then 3 4 5 again.

Bottom--><--Stack top stage: 2 1 3 4 5

At this time 1 is the second row, other locations can also be similar to get


Found a sub-problem

Because 1 can be any of the 1-n on the stage, may wish to set him up on the K, and then found that when considering this problem, consider which situation can be divided into three parts

The k-1, which is labeled 1, is the first of the 1 before the stage, and after 1, the N-k

Then look at the k-1 on stage before 1, they can use the same analysis method for a smaller range of independent analysis, after 1 n-k can also


1 on the stage of the K, his unhappy value is a[i] * (k-1), then as long as he knows all the cock Silk's not happy value of the sum of the minimum value of Sumpre, and he after all the cock Silk's unhappy value The sum of the sum of the minimum sum of Sumnext, the last 1-n on the minimum value is

A[i] * (k-1) + Sumpre + sumnext

Sumpre and Sumnext can be recursively analyzed in the same way.

Until it is accurate to an individual with an interval length of no further separation.


Define the function int solve (i, j) function return interval [I, j] the minimum value required on the topic, then

Solve (i, j) = min (Solve (i + 1, i + k-1) + solve (i + K, j) + a[i] * (k-1) + (Sum[j]-sum[i + k-1]) * k) (1 < = k <= j-i + 1)


interval [I, j]

I i + 1, i + 2, i + 3, ....., i + k-1 i + K .... i + K + + 1.........J

K-1 after the first K-j-i-K + One


Because the interval after section I [i + K, J] is considered internally independently at the time of analysis, because it is not known what the parent problem is when considering this sub-problem

So the optimal solution on [i + K, j] is only independent considering [i + K, j] of the minimum value, when synthesized to [I, j] this interval due to [I, i + k-1] This k is in front of them so

[i + K, j] This optimal solution is also added d (i + k) * k + D (i + K + 1) * k + ... + d (j) * k, D (x) for the Cock silk value of the x cock wire

If the prefix and the expression is the first J of the Cock Silk value and sum[j], the former i + k-1 of the Cock silk value and sum[i + k-1]

D (i + k) * k + D (i + K + 1) * k + ... + d (j) * k = (sum[j]-sum[i + k-1]) * k


Boundary

Found at k = 1 o'clock

Solve (i + 1, i) + solve (i + 1, j) + a[i] * (1-1) + (Sum[j]-sum[i]) * 1

At this point I was the first one in the interval [I, j], visible before him, from I + 1 to J, all behind him.

And there's a meaningless solve (i + 1, i) to make this value meaningful only to make it equal to 0

Solve (i + 1, j) + (Sum[j]-sum[i])


At k = j-i + 1 O'Clock

Solve (i + 1, j) + Solve (j + 1, J) + a[i] * (j-i) + (Sum[j]-sum[j]) * (j-i + 1);

This time I was in [I, J], the last of the range, from I + 1 to J all in front of him.

Then there was the solve (j + 1, j) meaningless, and it was obvious that he was going to be equal to 0, and because it was all in front of I, so [i + 1, j] all the cock filaments were not mended (Sum[j]-sum[i + k-1]) * k), of course 0

Solve (i + 1, j) + a[i] * (j-i)


Realize

Memory search, because of the system call stack, relatively slow


Memory Search # include <iostream> #include <cstring> #include <algorithm>using namespace Std;const int maxn = +, INF = 0x3f3f3f3f;int N, A[MAXN], SUM[MAXN], dp[maxn][maxn];int solve (int i, int j) {if (i > J) return 0;int A  NS = Dp[i][j];if (ans! =-1) return Ans;ans = inf;for (int k = 1; k <= j-i + 1; k++) {ans = min (ans, solve (i + 1, i + K-1) + Solve (i + K, j) + a[i] * (k-1) + (Sum[j]-sum[i + k-1]) * k); return dp[i][j] = ans;} int main () {int t;scanf ("%d", &t), for (int T = 1; t <= t; t++) {scanf ("%d", &n); sum[0] = 0;for (int i = 1; I < ; = N; i++) {scanf ("%d", &a[i]), sum[i] = Sum[i-1] + a[i];} Memset (DP,-1, sizeof (DP));p rintf ("Case #%d:%d\n", T, solve (1, n));} return 0;}



Open Array Direct recursion, soon

Image looks like this process, is the parent problem to cover the sub-problem process, each time the coverage is expanded, eventually expanded to N, will all the sub-problems of the

Solutions are covered, that is, all the sub-problems up to form the final problem of the solution, DP of the wonderful AH ah ah ah ah ah ...









Recursive # include <iostream> #include <cstring> #include <algorithm>using namespace std;const int MAXN = 100 +, INF = 0x3f3f3f3f;int N, A[MAXN], SUM[MAXN], Dp[maxn][maxn];int main () {int t;scanf ("%d", &t); for (int T = 1; t &L T;= T; t++) {scanf ("%d", &n); sum[0] = 0;for (int i = 1; I <= n; i++) {scanf ("%d", &a[i]), sum[i] = Sum[i-1] + a[i];} for (int i = 0, I <= N; i++) {for (int j = 0; J <= N; j + +) {if (i > J) dp[i][j] = 0;elsedp[i][j] = INF;}} /*l is the optimal solution of the sub-problem for the interval length to be calculated, so that the optimal solution of the larger interval is first, then the optimal solution of interval length is 1, then 2 ...  Until the entire interval [1, n]*/for (int l = 1; l <= N; l++) {for (int i = 1; I <= n; i++) {Int J = i + l-1;if (J > N) continue; Cross-border for (int k = 1; k <= j-i + 1; k++) {Dp[i][j] = min (Dp[i][j], dp[i + 1][i + k-1] + dp[i + k][j] + (k-1) * a[ I] + (Sum[j]-sum[i + k-1]) * k);}} printf ("Case #%d:%d\n", T, Dp[1][n]);} return 0;}



hdu4283 you is the one interval DP memory search or recursion

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.