HDU 4283 You Are the One (区间dp)

来源:互联网 发布:数据存储四种方式 编辑:程序博客网 时间:2024/05/20 21:19

题意:

告诉你n 个人 刚开始的顺序, 规定每个人是第k 个上场  ,  不开心值增加 (k-1) * Di  ,你可以改变他们上场顺序, 可以用一个栈(小黑屋)来暂时存放他们。 问最小的不开心值?

思路:

区间dp

令dp[i][j] 表示i~j 这个区间安排完的最小不开心值。

这个题在转移时有点CF-85D 那个线段树的味道。点击打开链接

我们在安排区间时, 始终让它们按照第1,2,3,,个上场, 不要让其他区间的点 影响到该区间。

这样就好转移了。

我们考虑第i 个人 啥时候上场。它可以 第1,2,,,j-i+1 时上场。

如果它第k 次上场, 那么根据栈的性质,  i 后面的连续k-1个人 一定先于它上场。  剩下的人 一定后于它上场。

那么前k-1个人  不会影响总区间的费用, 直接加上他们的状态即可。

第i个人上场 费用就是 a[i] * (k-1)。

剩下的j-i+1-k 个人, 他们的状态 (记为dp) 要加上一定数量的值,才能符合总区间的费用。

假设 后面有四个人  他们的费用是  a  b  c d(已经按顺序登场)

那么  总费用为, a*0 + 1*b + 2c + 3d

假设前面有k 个人。 那么费用变成了:

   a*(0+k) + b*(1+k) + c(2+k) + d*(3+k)  = a*0 + 1*b + 2*c + 3*d + (a+b+c+d)*k

所以  统计个前缀和 即可 转移了。


#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int T;const int maxn = 100 + 10;int a[maxn];int dp[maxn][maxn];int sum[maxn];int ks;int main(){    scanf("%d",&T);    while(T--){        int n;        scanf("%d",&n);        memset(dp,0x3f3f3f3f,sizeof dp);        for (int i = 1; i <= n; ++i){            scanf("%d",a+i);            sum[i] = sum[i-1] + a[i];        }        dp[n][n] = 0;        for (int i = 1; i < n; ++i){            dp[i][i] = 0;            dp[i][i+1] = min(a[i], a[i+1]);        }        for (int i = 3; i <= n; ++i){            for (int j = 1; j+i-1 <= n; ++j){                int fi = j;                int la = j+i-1;                for (int k = 1; k <= i; ++k){                    if (k == 1){                        dp[fi][la] = min(dp[fi][la], dp[fi+1][la] + sum[la] - sum[fi]);                    }                    else if (k == i){                        int t = dp[fi+1][la] + (i-1)*a[fi];                        dp[fi][la] = min(dp[fi][la], t );                    }                    else {                        int t = dp[fi+1][fi+1+k-1-1] + a[fi]*(k-1)+dp[fi+1+k-1][la] + k*(sum[la] - sum[fi+1+k-1-1]) ;                        dp[fi][la] = min(dp[fi][la], t);                    }                }            }        }        printf("Case #%d: %d\n", ++ks, dp[1][n]);    }    return 0;}


You Are the One

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


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  512345554322
 

Sample Output
Case #1: 20Case #2: 24
 

Source
2012 ACM/ICPC Asia Regional Tianjin Online
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:  4267 4268 4269 4270 4271 
 

Statistic | Submit | Discuss | Note

原创粉丝点击