Hdu 4283 You Are the One(区间dp)

题目链接

You Are the One

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1807    Accepted Submission(s): 853


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

题意:n个人排队上台,每个人有个屌丝值Di,每个人的不高兴值为:(在他前面上台的人的个数)*(他的屌丝值)。现在有一个小黑屋:导演可以把让一个人进人小黑屋,让他之后的人比他先上台,最先进入小黑屋的人最后离开。(一个人只能进入一次小黑屋)。问所有人的不高兴值的总和最少为多少?

题解:如果我们考虑第一个上台的人是谁,问题不好解决。反过来,如果我们考虑最后一个上台的人是谁,这个问题就可以解决了。对于队列中的1到n个人,假设第i个人最后上台,显然区间[1,i-1]和区间[i+1,n]可以分别看成独立的问题解决。所以,我们可以用动态规划解决这个问题。

用dp[l][r] 表示区间[l,r] 的人上完台(小黑屋中没有剩下这个区间的人)的最小不高兴值。

转移的时候,我们考虑该区间最后一个上台的人为i,那么:

dp[l][r]=min(dp[l][r],Di*(r-l)+dp[l][i-1]+dp[i+1][r]+(D(i+1)+D(i+2)+.....Dr)*(i-l)));

代码如下:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<string>
#include<stack>
#include<math.h>
#define nn 110
#define inff 0x3fffffff
#define eps 1e-8
#define mod 1000000007
typedef long long LL;
const LL inf64=LL(inff)*inff;
using namespace std;
int n;
int a[nn];
int dp[nn][nn];
int sum[nn];
int cost(int l,int r,int x)
{
    if(l>r)
        return 0;
    return (sum[r]-sum[l-1])*(l-x-1);
}
int dfs(int l,int r)
{
    if(l>r)
        return 0;
    if(dp[l][r]!=-1)
        return dp[l][r];
    if(l==r)
        return dp[l][r]=0;
    int i;
    dp[l][r]=inff;
    for(i=l;i<=r;i++)
    {
        dp[l][r]=min(dp[l][r],a[i]*(r-l)+dfs(l,i-1)+dfs(i+1,r)+cost(i+1,r,l));
    }
    return dp[l][r];
}
int main()
{
    int t,i;
    scanf("%d",&t);
    int cas=1;
    while(t--)
    {
        scanf("%d",&n);
        sum[0]=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum[i]=sum[i-1]+a[i];
        }
        memset(dp,-1,sizeof(dp));
        printf("Case #%d: ",cas++);
        printf("%d\n",dfs(1,n));
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值