You Are the One HDU - 4283(区间DP)

本文深入探讨了一款名为YouAretheOne的电视节目的数学问题,该节目旨在满足单身男生的需求。通过设置算法解决如何最小化男生们上场的不满意程度的问题,文章详细解释了区间DP方法的应用过程,包括定义状态转移方程,使用前缀和优化计算,以及最终求解最小不满度的方法。通过实例输入和输出展示了算法的具体实现步骤和结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You Are the One HDU - 4283

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
题意:

有一群屌丝,每个屌丝有个屌丝值,如果他第K个上场,屌丝值就为a[i]*(k-1),因为前面有k-1个人在他前面上台,可以把几个人拉进小黑屋来调整,可以把小黑屋当成栈因为先进去的人最后才能出来,使得最后总屌丝值最小

分析:

区间DP

定义dp[i][j]dp[i][j]表示原序列区间[i,j][i,j]的人的最小屌丝值

这样我们枚举一个中间变量k,设i在区间[i,j]中是第k个上场的

这样区间分成了两部分

dp[i+1][i+k1](k1i),dp[i+k][j]dp[i+1][i+k−1](k−1个人在i之前),dp[i+k][j](之后的人)

也就是让[i,i+k1][i,i+k−1]这些人进入小黑屋,i先进的,那么他前面将有k-1个人,他是第k个上场的,之后再让后面的[i+k,j][i+k,j]这些人上场

这样很明显i等待了k-1个人屌丝值增长为(k1)×a[i](k−1)×a[i],而[i+k,j][i+k,j]这些人则等待了k个人,屌丝值增长为k×k×(这些人的屌丝值之和)

因此状态转移公式为:

dp[i][j]=min(dp[i][j],dp[i+1][i+k1]+dp[i+k][j]+k×(sum[j]sum[i+k1])+a[i]×(k1))dp[i][j]=min(dp[i][j],dp[i+1][i+k−1]+dp[i+k][j]+k×(sum[j]−sum[i+k−1])+a[i]×(k−1))

sum[]记录了每个人屌丝值的前缀和

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
int n,a[105],sum[105],dp[105][105];

int main(){
    int t,cas = 0;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        memset(sum,0,sizeof(sum));
        for(int i = 1; i <= n; i++){
            scanf("%d",&a[i]);
            sum[i] = sum[i-1] + a[i];
        }
        memset(dp,0,sizeof(dp));
        for(int i = 1; i <= n; i++){
            for(int j = i + 1; j <= n; j++){
                dp[i][j] = INF;
            }
        }
        //注意初始化,dp[i][i]=0,其他长度的为INF
        for(int l = 1; l < n; l++){
            for(int i = 1; i + l <= n; i++){
                int j = i + l;
                for(int k = 1; k <= j - i + 1; k++){
                    dp[i][j] = min(dp[i][j],dp[i+1][i+k-1]+dp[i+k][j]+k*(sum[j]-sum[i+k-1])+a[i]*(k-1));
                }
            }
        }
        printf("Case #%d: %d\n",++cas,dp[1][n]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值