Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5543 Accepted Submission(s): 2682 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)
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 2012 ACM/ICPC Asia Regional Tianjin Online
Recommend liuyiding | We have carefully selected several similar problems for you: 4267 4268 4269 4270 4271 |
题目大意:有n个屌丝,每个都有一定的怒气值ai,进房间的顺序决定他们的最终怒气是(k-1)*ai,要求出他们进房间的最小怒气总和
思路:依旧是从小区间到大区间,例如dp【i】【j】,表示从i到j这j-i+1个人的最小出场怒气总和,这里就是第i个人先出场,不用考虑前面的,
那么对于这dp【i】【j】中的 第i个人,也就是第一个,有可能是第一个上场,也有可能是到j-i+1个上场,我们距离第k个上场,
那么就是dp[i+1][i+k-1]+dp[i+k][j]+(k-1)*a[i]+k*(sum[j]-sum[i+k-1]),绿色的这一块就表示你让第k个后面的小兄弟等了k+1次才上场,所以按照规则,每个人*k的怒气,
状态转移方程:dp[i][j]=min(dp[i][j],dp[i+1][i+k-1]+dp[i+k][j]+(k-1)*a[i]+k*(sum[j]-sum[i+k-1]))
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#define maxn 110
using namespace std;
const int inf=0x7fffffff;
int dp[maxn][maxn],sum[maxn],a[maxn];
int main()
{
int t, cas=1;
cin>>t;
while(t--)
{
int n;
cin>>n;
sum[0]=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum[i]=sum[i-1]+a[i];
}
for(int len=1;len<n;len++)
for(int i=1;i<=n;i++)
{
int j=i+len;
dp[i][j]=inf;
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]+k*(sum[j]-sum[i+k-1]));
}
printf("Case #%d: %d\n",cas++,dp[1][n]);
}
return 0;
}