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]);
        }
    }
    
  • 相关阅读:
    promise封装微信小程序的request
    css3实现loading效果--当页面加载过程中显示Loading的进度条,全部加载完成之后进度条消失
    解决H5微信浏览器中audio兼容-- 背景音乐无法自动播放
    h5微信浏览器复制粘贴--ios兼容问题的解决方法(clipboard.js插件)
    解决微信二次分享失败--后面被加上from=singlemessage&isappinstalled=0的解决方案
    h5微信分享
    mpvue微信小程序项目踩坑记录
    水塘抽样算法
    Redis批量删除脚本
    java9 模块化 jigsaw
  • 原文地址:https://www.cnblogs.com/Heilce/p/6547737.html
Copyright © 2011-2022 走看看