hdoj 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): 2600    Accepted Submission(s): 1198


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
 




题意:有N个人按顺序排成一排上台表演,每个都有一个num[]值,若在他是第k个上场的人,则会有num[]*(k-1)的unhappiness。台下有一个黑屋(stack),对每一个人,可以选择让他先进屋子或者直接上台。现在让你找到一个最优方案使得所有人的unhappiness之和最小。


思路:设置dp[i][j]为区间[i, j]的人上台表演最小的unhappy值之和。

考虑该区间所有的方案——我们枚举最后一个上台的人。

若最后一个上台的人是第k个人(i <= k <= j),那么区间[i, k-1]里面的人在他前面上台,同样的区间[k+1, j]里面的人也在他前面上台,这时需要考虑到区间[i, k-1]对区间[k+1, j]的影响,因为区间[i, k-1]里面的人是在区间[k+1, j]之前上台的。

这样dp[i][j] 划分成三个子区间[i, k-1] [k, k] [k+1, j]


dp[i][j]

= min(代价一:dp[i][k-1] + 代价二:num[k]*(j-i) + 代价三:dp[k+1][j]+(k-i)*sigma(num[l] 注:k+1<=l<=j));


还是记忆化好使,不用考虑边界。。。


AC代码:


#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (200+10)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
int ans[110][110];
int num[110], sum[110];
int dp(int i, int j)
{
    if(i >= j) return 0;
    if(ans[i][j] != -1) return ans[i][j];
    ans[i][j] = INF;
    for(int k = i; k <= j; k++)
        ans[i][j] = min(ans[i][j], dp(i, k-1)+(j-i)*num[k]+dp(k+1, j)+(sum[j]-sum[k])*(k-i));
    return ans[i][j];
}
int main()
{
    int t, kcase = 1;
    Ri(t);
    W(t)
    {
        int n; Ri(n);
        sum[0] = 0;
        for(int i = 1; i <= n; i++)
        {
            Ri(num[i]);
            sum[i] = sum[i-1] + num[i];
        }
        CLR(ans, -1);
        printf("Case #%d: %d\n", kcase++, dp(1, n));
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值