You Are the One
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6113 Accepted Submission(s): 2982
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
题意:像《非诚勿扰》这样的电视剧很受欢迎。
为了满足单身男孩的需求,TJUT举办了自己的节目。
演出在小礼堂举行,所以吸引了很多男孩和女孩。
现在有n个男孩报名。
一开始,n个男孩站成一排,一个一个走向舞台。
然而,导演突然知道,每个男孩都有屌丝D的价值,如果男孩是k第一个上舞台的,他的不快乐将是(k-1)*D,因为他要等(k-1)人。
幸运的是,在小大厅里有一个黑暗的房间,所以导演可以把男孩暂时放到黑暗的房间里,让他身后的男孩先上台。
因为黑洞洞的房间很窄,第一个进入黑洞洞的男孩必须在最后离开。
导演想通过暗室改变男生的顺序,所以对不开心的总结最少。
你能帮助他吗?
为了满足单身男孩的需求,TJUT举办了自己的节目。
演出在小礼堂举行,所以吸引了很多男孩和女孩。
现在有n个男孩报名。
一开始,n个男孩站成一排,一个一个走向舞台。
然而,导演突然知道,每个男孩都有屌丝D的价值,如果男孩是k第一个上舞台的,他的不快乐将是(k-1)*D,因为他要等(k-1)人。
幸运的是,在小大厅里有一个黑暗的房间,所以导演可以把男孩暂时放到黑暗的房间里,让他身后的男孩先上台。
因为黑洞洞的房间很窄,第一个进入黑洞洞的男孩必须在最后离开。
导演想通过暗室改变男生的顺序,所以对不开心的总结最少。
你能帮助他吗?
思路:我们设dp[i][j]为当队伍里只有i到j的人时不开心之和的最小值
我们由题易知对于区间(i,j),当i号第k个出场时,那么(i+1,i+k)区间里的人肯定比i先出场(因为i要第k个出场,那么i肯定是先进小黑屋了,(i+1,i+k)区间的人要不进小黑屋就是先出场,所以他们肯定是比i先出场的)
而(i+k+1,j)区间内的人肯定是比i后出场的,我们就可以得出状态转移方程(sum[i]表示前i个人屌丝D之和)
dp[i][j]=min(dp[i][j], dp[i+1][i+k] + d[i]*(k-1) + (sum[j]-sum[i+k])*k +dp[i+k+1][j] )
比i先出场 i出场 此时i后面人的已经等了这么久 比i后出场
代码
#include<cstdio> #include<algorithm> using namespace std; int s[110]; int sum[110];//前缀和 const int INF=1e9; int dp[110][110]; int main(){ int t,k=1; scanf("%d",&t); while(t--){ int n; scanf("%d",&n); sum[0]=0; for(int i=1;i<=n;i++){ scanf("%d",&s[i]); sum[i]=sum[i-1]+s[i]; dp[i][i]=0; } for(int i=1;i<n;i++){ for(int j=1;i+j<=n;j++){ dp[j][i+j]=INF; for(int k=j;k<=i+j;k++){ //printf("%d ",dp[j][i+j]); dp[j][i+j]=min(dp[j][i+j],dp[j+1][k]+(k-j)*s[j]+(k-j+1)*(sum[i+j]-sum[k])+dp[k+1][i+j]); //printf("%d %d %d %d %d %d %d ",k,dp[j+1][k],(k-j)*s[j],(i-k+j),(sum[i+j]-sum[k]),dp[k+1][i+j],dp[j][i+j]); } //printf("ww%d %d %d %d ",i,j,i+j,dp[j][i+j]); } } printf("Case #%d: %d ",k++,dp[1][n]); } return 0; }