• 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;
    }
  • 相关阅读:
    iOS常用框架总结
    【Java】使用@Value @Reource或@Autowire依赖 (值) 注入时出现NPE的排查方法
    【Java】事件驱动模型和观察者模式
    新人训练营心得 - 道路阻且长
    【Java】Spring Web MVC注意事项
    【Linux】OpenWRT的无线设置注意事项——从2.4G到5G,hwmode不简单
    【Java】 Spring依赖注入小试牛刀:编写第一个Spring ApplicationContext Demo
    【Linux】 awk应用
    【C/C++】高亮C++中函数的重写——函数名相同?参数列表相同?返回值相同?
    【设计模式】C++单例模式的几种写法——Java自动加载内部类对象,C++怎么破?
  • 原文地址:https://www.cnblogs.com/hrlsm/p/13332714.html
Copyright © 2020-2023  润新知