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]);
        }
    }
    
  • 相关阅读:
    redis 秒杀设计,常用命令
    wamp下配置多域名和访问路径的方法
    git commit 因执行yarn , npm,报错推送不了
    taro bug--小程序报错Error: 未找到入口 sitemap.json 文件,或者文件读取失败,请检查后重新编译。
    vscode引用相对路径,scss时路径报错问题解决
    space-between与space-around的区别
    Android可设置任意高度的TextView,如:设置0.5px设置0.1px等等
    Android可以打开微信支付,但是没法调起小程序支付
    dask
    Joblib-lightweight piplining tool
  • 原文地址:https://www.cnblogs.com/Heilce/p/6547737.html
Copyright © 2011-2022 走看看