HDU 4283 --- You Are the One (interval DP), hdu4283 --- you
Question Link
Http://acm.split.hdu.edu.cn/showproblem.php? Pid = 1, 4283
Problem DescriptionThe TV shows such as You Are the One has been very popular. in order to meet the need of boys who are still single, TJUT hold the show itself. the show is hold in the Small hall, so it attract a lot of boys and girls. now there are 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 has a value of diaosi D, if the boy is k-th one go to the stage, the unhappiness of him will be (k-1) * D, because he has to wait for (k-1) people. luckily, there is a dark room in the Small hall, so the director can put the boy into the dark room temporarily and let the boys behind his go to stage before him. for the dark room is very narrow, the boy who first get into dark room has to leave last. the director wants Change the order of boys by the dark room, so the summary of unhappiness will be least. Can you help him?
InputThe 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 are n integer D1-Dn means the value of diaosi of boys (0 <= Di <= 100)
OutputFor each test case, output the least summary of unhappiness.
Sample Input2 512345554322
Sample OutputCase #1: 20 Case #2: 24
Source2012 ACM/ICPC Asia Regional Tianjin Online
RecommendLiuyiding | We have carefully selected several similar problems for you: 4267 4268 4269 4270 4271 question: there are n people, enter n base values (anger values) v [], show everyone's anger value, now they come on stage in turn, if I is the k on stage, then his anger value is (k-1) * v [I] Now there is a stack, let some people go to the stack and let the people later come to the stage to perform performances. This will change some of the order and ask everyone for the smallest anger value and the train of thought: interval DP, define dp [I] [j] to indicate the sum of the minimum angry values of the person from I to j in the original sequence, and set I to play the k in the range I to j, then I + 1 ~ I + k-1 will first I on stage, then I on stage, finally I + k ~ I + len came on stage, then the state transition equation is: dp [I] [j] = v [I] * (k-1) + dp [I + 1] [I + k-1] + (sum [I + len]-sum [I + k-1]) * k + dp [I + k] [I + len] sum [] indicates the prefix and the code is as follows:
# Include <iostream> # include <algorithm> # include <cstdio> # include <cstring> # include <set> using namespace std; const int inf = 0x7fffffff; int v [105]; int dp [105] [105]; int sum [105]; int main () {int T, n, Case = 1; cin> T; while (T --) {scanf ("% d", & n); sum [0] = 0; for (int I = 1; I <= n; I ++) {scanf ("% d", & v [I]); sum [I] = sum [I-1] + v [I];} for (int I = 1; I <= n; I ++) dp [I] [I] = 0; for (int len = 1; len <n; len ++) {for (int I = 1; I + len <= n; I ++) {// Special I in I ~ I + len is the first and last time in the interval; dp [I] [I + len] = min (sum [I + len]-sum [I] + dp [I + 1] [I + len], dp [I + 1] [I + len] + v [I] * len); for (int k = 2; k <= len; k ++) dp [I] [I + len] = min (dp [I] [I + len], v [I] * (k-1) + dp [I + 1] [I + k-1] + (sum [I + len]-sum [I + k-1]) * k + dp [I + k] [I + len]) ;}} printf ("Case # % d: % d \ n", Case ++, dp [1] [n]) ;}}