You Are the One
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3458 Accepted Submission(s): 1590
Problem Description
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)
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
Source
Recommend
题意:每个人有个愤怒值,如果他第k个上场那么获得的愤怒值为a[k]*(k-1),每个人按顺序进入一个堆栈,但可通过调整出堆栈顺序来调整出场顺序,问总的愤怒值最小为多少。
思路:区间dp,这题算是挺难想的了,dp[i][j]表示区间愤怒最小值,那么对与每个人来说他可能第一个出场也可能最后一个出场,假设第i个人第k个出场,那么第i+1个到第i+k-1个人是前k个出场的,愤怒值为dp[i+1][i+k-1],第i个人的愤怒值为a[i]*(k-1),那么对于第i+k个人到j个人的情况又怎么办呢,因为每个人晚一位出场愤怒值加的是定值,现在第i+k个人到j个人每个人都晚了k位出场,那么只要用dp[i+k][j]+k*(他们愤怒值的总和)就是所需要的答案,那么就能得到动态转移方程:dp[i][j]=min(dp[i][j],dp[i+1][i+k-1]+a[i]*(k-1)+dp[i+k][j]+k*(他们的愤怒值总和)),下面给代码:
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<bitset>
#include <utility>
using namespace std;
#define maxn 105
#define inf 0x3f3f3f3f
typedef long long LL;
const int mod = 1e8;
int dp[maxn][maxn], a[maxn], sum[maxn];
int main(){
int t;
scanf("%d", &t);
for (int tcase = 1; tcase <= t; tcase++){
int n;
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, 0, sizeof(dp));
for (int i = 1; i <= n; i++){
for (int j = i + 1; j <= n; j++){
dp[i][j] = inf;
}
}
for (int i = 0; i < n; i++){
for (int j = 1; j <= n; j++){
for (int k = 1; k <= i + 1; k++){
dp[j][j + i] = min(dp[j][j + i], dp[j + 1][j + k - 1] + (k - 1)*a[j] + dp[j + k][j + i] + (sum[j + i] - sum[j + k - 1])*k);
}
}
}
printf("Case #%d: %d\n", tcase, dp[1][n]);
}
}