zoukankan      html  css  js  c++  java
  • HDU4283:You Are the One(区间DP)

    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
     
    表示这个问题真的很难理解啊,有可能我的理解能力真的是太差了,但是。。。。
    这个题就是基础的区间dp,就是在区间[i,j]中i出现的次序就是区间[i+1,k]和[k+1,j]之间利用这个条件写出来区间dp的状态的转移的方程。
    #include<bits/stdc++.h>
    using namespace std;
    const int INF=1e9;
    long long dp[1000][1000];
    int a[1000];
    int sum[1000];
    int main()
    {
        int t;
        int ans=0;
        scanf("%d",&t);
        while(t--)
        {
            ans++;
            int n;
            scanf("%d",&n);
            for(int i=1;i<=n;i++) scanf("%d",&a[i]);
            memset(sum,0,sizeof(sum));
            for(int i=1;i<=n;i++)
            {
                sum[i]+=sum[i-1]+a[i];
            }
            for(int i=1;i<=100;i++)
            {
                for(int j=1;j<=100;j++)
                {
                    if(j>i)
                    dp[i][j]=INF;
                    else dp[i][j]=0;
                }
            }
            //memset(dp,0,sizeof(dp));
            for( int len=1;len<=n;len++)
            {
                for(int i=1;i<=n;i++)
                {
                    if(i+len-1>n) break;
                    if(len==1) {dp[i][i]=0; continue;}
                    else
                    {
                        for(int k=i;k<=i+len-1;k++)
                        {
                            long long x=dp[i+1][k]+(k-i)*a[i]+(k-i+1)*(sum[i+len-1]-sum[k])+dp[k+1][i+len-1];
                            dp[i][i+len-1]=min(dp[i][i+len-1],x);
                        }
                    }
                }
            }
            //cout<<dp[2][5]<<endl;
            printf("Case #%d: ",ans);
            printf("%lld
    ",dp[1][n]);
        }
    }
    
  • 相关阅读:
    (转)Pythonfunctools详解
    (转)这有 73 个例子,彻底掌握 fstring 用法!
    (转)你不一定全知道的四种Python装饰器实现详解
    (转)学习Python,怎能不懂点PEP呢?
    (转)5个案例详解装饰器 | 手把手教你入门Python之四十六
    (转)Python 对象协议
    (转)functools — 函数操作工具箱
    (转)Python 魔术方法指南
    Python 对象协议
    (转)Python timeit模块的使用实践
  • 原文地址:https://www.cnblogs.com/Heilce/p/6547737.html
Copyright © 2011-2022 走看看