HDU - 4283You 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): 4280 Accepted Submission(s): 1989

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

Recommend

liuyiding

题意:

给定一个序列,可以根据栈的规则调换顺序,求使得每个人的不开心度最小。
考虑最简单的区间的情况,如果L-K,K+1-R单独计算,
那么总不开心度由0*a[L]+1*a[L+1]+2*a[L+2]+……+(K-L)*a[K],0*a[k+1]+1*a[k+2]+2*a[k+3]+……+(R-k-1)*a[R],
而如果顺序合并代价应该是0*a[L]+1*a[L+1]+2*a[L+2]+……+(R-L-1-1)*a[R],
发现两者的差异是k*(a[k+1]+a[k+2]+a[k+3]+……a[R]);
如果反序也有类似的结论,
因此我们只要单独计算算每个区间的代价,合并时有前缀和来维护递推即可
由此推广到任意区间

***DP[I[]J]=max{DP[I+1][K]+a[I](k-1)+DP[k+1][J]+k*(a[k+1]+….+a[J])}
(枚举第I个人在I-K区间内是第K个上台,那么I+1-K必须是前K-1个上台,与DP的意义相同,代价就是DP[I+1][K]+a[I](k-1),后半段区间[K+1,J]整体被平移了K个单位时间,代价是DP[K+1][J]+K(a[k+1]+……+a[j]));**

#include <cstdio>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <set>
#include <bitset>
#include <map>
#define LL long long
#define ULL unsigned long long
#define mem(a,n) memset(a,n,sizeof(a))
#define fread freopen("in.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
#define N 110
#define INF 0x3f3f3f3f
#define eps 1e-9
using namespace std;
int num[N],sum[N],dp[N][N];
int main()
{
    int t;
    int n;
    scanf("%d",&t);
    for(int jase=1;jase<=t;++jase){
        scanf("%d",&n);
        for(int i=1;i<=n;++i){
            scanf("%d",num+i);
            sum[i]=sum[i-1]+num[i];
        }
        mem(dp,0);
        for(int len=1;len<n;++len)
        for(int i=1;i+len<=n;++i){
            dp[i][i+len]=sum[i+len]-sum[i]+dp[i+1][i+len];
            for(int j=i+1;j<=i+len;++j){
                dp[i][i+len]=min(dp[i][i+len],num[i]*(j-i)+dp[i+1][j]+(sum[i+len]-sum[j])*(j-i+1)+dp[j+1][i+len]);
            }
        }
        printf("Case #%d: %d\n",jase,dp[1][n]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值