HDU4283 You Are the One (interval dp)

Source: Internet
Author: User
Tags cmath

HDU4283 You Are the One (interval dp)
You Are the OneTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission (s): 2714 Accepted Submission (s): 1247

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 h As 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. T He 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 4 3 2 2 

Sample Output
Case #1: 20 Case #2: 24 

N People Press 1, 2, 3... the order of n is well arranged, and each person has an unhappy value; if the I-th person k came on stage, then his unhappy value is (k-1) * unhappy [I]. They need to go through a small black house before getting on stage (that's equivalent to a stack ...) Analysis: it cannot be written using stacks. If it is used, it will be wrong. Actually, it is a range of dp, which is more difficult. dp [I] [j] indicates the range [I, j] minimum unhappy value. For dp [I] [j], the I-th player may play 1st or j-I + 1. Consider playing the K
That is, after I + 1 K-1 is the first person to play, so there is a sub-problem dp [I + 1] [I + 1 + k-1-1] That played before I
For the I-th person, as the k-th player, the angry value is a [I] * (k-1)
Other people come out after k + 1, that is, a subproblem dp [I + k] [j]. For those in this range, because it is placed after k + 1, the overall anger value must be added (sum [j]-sum [I + k-1]) * k.
<span style="font-size:18px;">#include <iostream>#include <cstdio>#include <cstring>#include <stack>#include <queue>#include<map>#include <set>#include <vector>#include <cmath>#include <algorithm>using namespace std;const double eps = 1e-6;const double pi = acos(-1.0);const int INF = 1<<27;const int MOD = 1000000007;#define ll long long#define CL(a,b) memset(a,b,sizeof(a))#define MAXN 100010int a[105],sum[105],dp[105][105];int main(){    int T,n;    scanf("%d",&T);    for(int cas=1; cas<=T; cas++)    {        scanf("%d",&n);        sum[0] = 0;        for(int i=1; i<=n; i++)        {            scanf("%d",&a[i]);            sum[i] = sum[i-1] + a[i];        }        CL(dp, 0);        for(int i=1; i<=n; i++)            for(int j=i+1; j<=n; j++)                dp[i][j] = INF;        for(int l=1; l<=n-1; l++)        {            for(int i=1; i<=n-l; i++)            {                int j = i+l;                for(int k=i; k<=j; k++)                    dp[i][j] = min(dp[i][j], dp[i+1][k]+dp[k+1][j]+a[i]*(k-i)+(sum[j]-sum[k])*(k-i+1));            }        }        printf("Case #%d: %d\n",cas,dp[1][n]);    }    return 0;}</algorithm></cmath></vector></set></map></queue></stack></cstring></cstdio></iostream></span>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.