You Are the One
The 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 to change the order of boys by the dark room, so the summary of unhappiness will 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 are 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 Input2 5 1 2 3 4 5 5 5 4 3 2 2Sample Output
Case #1: 20 Case #2: 24
区间dp,第一个数想在第n位输出需要把前n个数输出,再输出后n个,然后补上一个差值,因为输出后面的需要补上前面的n个数带来的位次
样例有些问题,建议手动输入
区间从后往前dp,感觉可以用滚动数组优化,不过ac了就不优化了(笑)
#define _CRT_SECURE_NO_WARNINGS #include<cstdlib> #include<iostream> using namespace std; #define x 1000000 int min(int a, int b) { return a < b ? a : b; } int main() { int m = 0; cin >> m; for (int y = 1; y <= m; y++) { int n; cin >> n; int a[101] = { 0 }; int sum[101] = { 0 }; for (int i = 1; i <= n; i++) { cin >> a[i]; sum[i] = sum[i - 1] + a[i]; } int map[101][101] = { 0 }; for (int i = n - 1; i >= 0; i--) { for (int j = n; j > i; j--) { map[i][j] = 99999999; for (int k = 1; k <= j - i + 1; k++) { map[i][j] = min(map[i][j], map[i + 1][i + k - 1] + map[i + k][j] + (k - 1) * a[i] + (sum[j] - sum[i + k - 1]) * k); } } } cout << "Case #"<<y<<": " << map[1][n] << endl; } return 0; }
一天不做题,浑身难受

浙公网安备 33010602011771号