您的位置:首页 > 其它

hdu 4283 You Are the One(区间dp)

2015-11-15 17:17 591 查看


You Are the One

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


给定一群人,每个人有一个怒气值Di,这群人要上场,越晚上怒气值越高,第i个人如果是第k个出场,那么最后的怒气值总值增加Di * (k-1), 如果第i个人暂时不让他出场,可以把他放小黑屋,让其后面的人上场,求最小的总怒气值。

区间DP:对dp[i][j],我们考虑i到j的(j-i+1)个人,对于第i个人我们可以假设他在第k个位置,则前面就有k-1个人在他前面,j-k个人在他后面,那么现在区间就变成了两个子区间[i+1][k],[k+1][j],并且在k后面出场的,至少都是k+1个上场。

dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]+w[i]*(k-i)+(sum[j]-sum[k])*(k-i+1));

其中sum[i]为前i个人的怒气值,k-i+1是指在这个i到j的区间中相对是第几个,后面的j-k个人的出场次序至少是K+1开始往上增加,那么相对于区间[j-k+1][j]来说其怒气值至少增长K*其怒气值。感觉不好说清楚,自己思考思考,理解就好了。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int Max=100005;
const int INF=0x3f3f3f3f;
int w[110],sum[110],dp[110][110],n;

int main()
{
int t;
scanf("%d",&t);
for(int k=1;k<=t;k++)
{
scanf("%d",&n);
for (int i=0; i<=n; i++)
{
for (int j=0; j<=n; j++)
{
if (j>i)
dp[i][j]=INF;
else
dp[i][j]=0;
}
}
sum[0]=0;
for (int i=1; i<=n; i++)
{
scanf("%d",&w[i]);
sum[i]=sum[i-1]+w[i];
}
int End;
for (int len=1; len<=n; len++) //区间长度(dp[i][j]区间长度为j-i)
{
for (int i=1; i+len<=n; i++) //区间起始点
{
End=i+len;
for(int j=i;j<=End;j++) //枚举放的位置
dp[i][End]=min(dp[i][End],dp[i+1][j]+dp[j+1][End]+w[i]*(j-i)+(sum[End]-sum[j])*(j-i+1));
}
}
printf("Case #%d: %d\n",k,dp[1]
);
}
return 0;
}


记忆化搜索:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int Max=100005;
const int INF=0x3f3f3f3f;
int w[110],sum[110],dp[110][110],n;

int dfs(int x,int y) //区间[x][y]
{
if (dp[x][y]!=INF)
return dp[x][y];

if (x>y)
return dp[x][y]=0;
for (int i=x; i<=y; i++) //枚举出场时间
dp[x][y]=min(dp[x][y],dfs(x+1, i)+dfs(i+1, y)+w[x]*(i-x)+(sum[y]-sum[i])*(i-x+1));
return dp[x][y];
}

int main()
{
int t;
scanf("%d",&t);
for(int k=1;k<=t;k++)
{
scanf("%d",&n);
for (int i=0; i<=n; i++)
{
for (int j=i; j<=n; j++)
dp[i][j]=INF;
}
sum[0]=0;
for (int i=1; i<=n; i++)
{
scanf("%d",&w[i]);
sum[i]=sum[i-1]+w[i];
}

printf("Case #%d: %d\n",k,dfs(1,n));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: