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]);
        }
    }
    
  • 相关阅读:
    javascript基础学习三---DOM操作
    小程序-微信开发者工具使用
    回溯算法实践--工作分配问题
    回溯算法理解
    贪心算法--删数问题
    单线程与多线程的区别
    【图解】Web前端实现类似Excel的电子表格
    详细了解JS Map,它和传统对象有什么区别?
    带你彻底弄懂nth-of-type与nth-child的区别
    input 纯数字输入 限制长度 限制 最大值
  • 原文地址:https://www.cnblogs.com/Heilce/p/6547737.html
Copyright © 2011-2022 走看看