hdu4283(區間dp)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   java   os   strong   

You Are the One

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1585    Accepted Submission(s): 756


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 Input2  512345554322 

 

Sample OutputCase #1: 20Case #2: 24 總結:最開始的時候,我採用的是貪心的思想,仔細想想貪心肯定不對。其實是一個區間dp,dp[i][j]表示第i個人到第j個人這個區間的最小花費(只考慮j-i+1個人,不需要考慮在它前面有多少人)對於dp[i][j]的第i個人可能第一個上場,也有可能第j-i+1個上場,考慮其第k個上場,那麼i+1之後的k-1個人首先上場,那麼就出現了一個子問題 dp[i+1][i+1+k-1-1]表示在第i個人之前上場的
對於第i個人,由於是第k個上場的,那麼憤怒值便是a[i]*(k-1)
其餘的人是排在第k+1個之後出場的,也就是一個子問題dp[i+k][j],對於這個區間的人,由於排在第k+1個之後,所以整體憤怒值要加上k*(sigma(i+k--j))
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 const int maxn = 105; 7 const int oo = 99999999; 8 int dp[maxn][maxn]; 9 int a[maxn],sum[maxn];10 int main()11 {12     int T,t,n;13     scanf("%d",&T);14     for (t = 1; t<=T; t++)15     {16         scanf("%d",&n);17         for (int i=1; i<=n; i++)18             scanf("%d",&a[i]);19         sum[0] = 0;20         for (int i=1; i<=n; i++)21             sum[i]=sum[i-1]+a[i];22         memset(dp,0,sizeof(dp));23         for (int i=1; i<=n; i++)24             for (int j=i+1; j<=n; j++)25                 dp[i][j]=oo;26         for (int len=1; len<n; len++)27         {28             for (int i=1; i<=n-len; i++)29             {30                 int j = i + len;31                 for (int k=1; k<=j-i+1; k++)32                     dp[i][j]=min(dp[i][j],dp[i+1][i+k-1]+dp[i+k][j]+(k-1)*a[i]+k*(sum[j]-sum[i+k-1]));33             }34         }35         printf("Case #%d: %d\n",t,dp[1][n]);36     }37     return 0;38 }
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.