题目链接
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)
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
题意:给定一个序列,序列内的人有屌丝值Di,第i个人如果是第k个出场,那么他的屌丝值为Di * (k-1), 但是导演可以通过一个栈来调整序列里面人的出场顺序。
求一个出场序列使总屌丝值最小。
#include<bits/stdc++.h>
using namespace std;
const int maxn =205;
typedef long long ll;
const ll inf=1e18;
ll sum[maxn],dp[maxn][maxn],a[maxn];
int main()
{
int T,x,n;
scanf("%d",&T);
for(int s=1;s<=T;++s)
{
scanf("%d",&n);
sum[0]=0;
for(int i=1;i<=n;++i) scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i];
for(int i=1;i<=n;++i)
{
for(int j=1;j<=n;++j) dp[i][j]=inf;
dp[i][i]=0;
}
for(int len=2;len<=n;++len)
{
for(int l=1;l+len-1<=n;++l)
{
int r=l+len-1;
dp[l][r]=min(dp[l+1][r]+sum[r]-sum[l],dp[l+1][r]+(r-l)*a[l]);
for(int k=l+1;k<r;++k)
dp[l][r]=min(dp[l][r],dp[l+1][k]+a[l]*(k-l)+dp[k+1][r]+(sum[r]-sum[k])*(k-l+1));
}
}
printf("Case #%d: %lld\n",s,dp[1][n]);
}
}
探讨一个通过调整序列中个体的出场顺序,以最小化总屌丝值的算法问题。利用动态规划方法,考虑如何使用栈来优化序列,减少等待导致的不幸福感。
3万+

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



