题目链接:Problem - 4283 (hdu.edu.cn)
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 Input
2
5
1 2 3 4 5
5
5 4 3 2 2
Sample Output
Case #1: 20
Case #2: 24
思路:区间dp
#include<bits/stdc++.h>
using namespace std;
const long long inf = 1e15;
long long arr[105];
long long dp[105][105];
long long sum[105];
int main(){
int t;
int n;
cin >> t;
int ans = 1;
while(t--){
cin >> n;
arr[0] = 0;
arr[n + 1] = 0;
sum[0] = 0;
for(int i = 1; i <= n; i++){
cin >> arr[i];
sum[i] = sum[i - 1] + arr[i];
}
for(int i = 0; i <= n + 1; i++){
for(int j = 0; j <= n + 1; j++){
if(i < j){
dp[i][j] = inf;
}else if(i == j){
dp[i][j] = 0;
}else{
dp[i][j] = 0;
}
}
}
for(int len = 2; len <= n; len ++){
for(int i = 1; i <= n; i++){
int j = i + len - 1;
if(j > n){
break;
}
for(int k = i; k <= j; k++){
dp[i][j] = min((k - i) * arr[i] + dp[i + 1][k] + dp[k + 1][j] + (k + 1 - i) * (sum[j] - sum[k]), dp[i][j]);
}
}
}
cout << "Case #" << ans++ << ": ";
cout << dp[1][n] << endl;
}
return 0;
}
这是一个关于优化问题的算法题目,背景设定类似于电视节目《非诚勿扰》。给定每个单身男士的价值(diaosi D),当男士k第几个上台时,他的不快乐值为(k-1)*D,因为要等待(k-1)个人。题目要求通过使用一个暗房来调整男士的上台顺序,使得总的不快乐值达到最小。算法采用区间动态规划求解,输入包含测试用例数量、男士人数及其价值,输出是最小的总不快乐值。

581

被折叠的 条评论
为什么被折叠?



