HDU4283-You Are the One(区间dp 模拟栈)

题目链接

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个人,每个人都有一个diaosi程度值,n个人入场有个次序,第一个进的diaosi值*0,第二个diaosi值*1,... ,最后一个diaosi值*(n-1),是他们每个人的不开心值,求所有人的不开心值的最小值。提供了用一个栈来改变最开始给的顺序。

思路

寻找dp[ i ] [ j ]和子区间的关系,由于可以用栈,想到在子区间和dp[ i ] [ j ]也应该有栈的操作。

对于区间 i 到 j ,第一个人(下标为i)可以第一个上台,也可以最后一个上台 ,也可以在中间某个位置上台,不外乎这几种情况。第一个人第几个上台的情况总共有(j - i + 1)种情况(区间长度),将这些情况全部求出来比较,求最小值。

 蓝色表示第一个人,选择在第k+1出场。对于蓝色,往后了k个位置,不开心值会加 a[ i ] * k; 对于黄色,dp的值是只有黄色自己区间的,现在整体往后了i+k个位置,用sum保存前 i 项和,查值就是黄色区间的和,不开心增加值就是 (sum[j]-sum[i+k])*(k+1); 再加上绿色和黄色子区间的dp值,就是一种情况的dp值。

用k在0到 L-1循环(L是区间长度),求最小值,就是dp[ i ] [ j ] .

注意求最小值,要先设置一个inf,在循环中再不断求最小值。

参考博客


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

int x,n,a[110],dp[110][110];
int sum[110];

int main()
{
    int T; scanf("%d",&T);
    int tt=1;
    while(T--)
    {
       scanf("%d",&n);
       memset(sum,0,sizeof(sum));
       memset(dp,0,sizeof(dp));
       for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            sum[i] = sum[i-1] + a[i];
       }
       for(int l=2;l<=n;l++){
            for(int i=1;i<=n;i++){
                int j = i+l-1;
                if(j>n) continue;
                int mmin = inf;
                for(int k=0;k<l;k++){
                    mmin = min(mmin,dp[i+1][i+k]+dp[i+k+1][j]+(sum[j]-sum[i+k])*(k+1)+a[i]*k);
                }
                dp[i][j] = mmin;
            }
       }
       printf("Case #%d: %d\n",tt++,dp[1][n]);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值