• You Are the One solution


    question:

    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

    solution:

    1.在一个区间 [l, j] 内,如果l是第 k (1<=k<=j-l+1)个出场的,那么 [l+1, l+k-1] 必定是先与l出场的,剩下的 [l+k, j] 是在l之后出场的。

    2.如果确定了l在区间 [l, j] 是第k个出场的,可以把区间分为 [l+1, l+k-1] 和 [l+k, j],两个区间互不影响,也可以分别对这两个区间单独求值。

    其在当前区间内是第k个出场的,先得到 D[l]*(k-1)。对于区间[l+1, l+k-1]的人,是先于l出场的,对于总体,他们是没有延迟的,直接加上dp[l+1, l+k-1]。对于区间[l+k, j]的人,他们是在l之后出场的,l在这个区间内又是第k个出场的,对于 [l+k, j]这个总体,他们是整体延迟了k个人的,加上 k*(sum[j]-sum[l+k-1]),加上了延迟所带来的值就可以把 区间[l+k, j]的出场是没有延迟的,情况跟区间[l+1, l+k-1]一样,再加上dp[l+k, j]。

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define N 1001
    #define INF 0x3f3f3f3f
    using namespace std;
    int D[N];
    int sum[N];
    int dp[N][N];
    int main()
    {
        int T;
        scanf("%d",&T);
        int Case=1;
        while(T--)
        {
            int n;
            sum[0]=0;
            scanf("%d",&n);
            memset(dp,0,sizeof(dp));
            memset(sum,0,sizeof(sum));
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&D[i]);
                sum[i]=sum[i-1]+D[i];
            }
            
            for(int len=1;len<n;len++)
            {
                for(int i=1;i<=n;i++)
                {
                    int j=i+len;
                    if(j<=i)
                    dp[i][j]=0;
                    else
                    dp[i][j]=INF;
                    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-1)*D[i]+k*(sum[j]-sum[i+k-1]));
                }
            }
            printf("Case #%d: %d
    ",Case++,dp[1][n]);  
        }
        return 0;
    }
  • 相关阅读:
    殷浩详解DDD:如何避免写流水账代码?
    如何从 0 到 1 开发 PyFlink API 作业
    探秘RocketMQ源码——Series1:Producer视角看事务消息
    教父郭盛华透露:PHP编程语言中多个代码执行缺陷
    互联网用户仍然容易受到黑客社会工程学攻击
    揭秘郭盛华在世界的排名,才华与颜值并存的男神
    什么是逆向工程?黑客是如何构建可利用的漏洞?
    人工智能时代,计算机网络主要面临哪些安全威胁?
    【2020-10-01】国庆堵车不堵心
    【2020-09-30】走起来慢,但实际很快
  • 原文地址:https://www.cnblogs.com/hrlsm/p/13332714.html
Copyright © 2020-2023  润新知