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\n",dp[1][n]);
    }
}
时间: 2024-08-12 23:14:26

HDU4283:You Are the One(区间DP)的相关文章

HDU-4283 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 ar

hdu4283 You Are the One 区间dp 记忆化搜索or递推

You Are the One Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3032    Accepted Submission(s): 1352 Problem Description The TV shows such as You Are the One has been very popular. In order to

hdu4283(区间dp)

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 me

区间DP基础篇之 HDU4283——You Are the One(非诚勿扰)

题目大意: 有n个男屌丝事先按1,2,3,,,,,,n的顺序排好,每个人都有一个不开心值unhappy[i],如果第i个人第k个上台找对象,那么该屌丝男的不开心值就会为(k-1)*unhappy[i],因为在他前面有k-1个人嘛,导演为了让所有男屌的总不开心值最小,搞了一个小黑屋,可以通过小黑屋来改变男屌的出场顺序 注意:这个小黑屋是个栈,男屌的顺序是排好了的,但是可以通过入栈出栈来改变男屌的出场顺序 解题思路:(操度娘所知~度娘你好腻害) dp[i][j]表示区间[i,j]的最小总不开心值 把

hdu4283 区间dp

1 //Accepted 300 KB 0 ms 2 //区间dp 3 //dp[i][j] 表示i到j第一个出场的最小diaosizhi 4 //对于i到j考虑元素i 5 //(1)i第一个出场,diaosizhi为 dp[i+1][j]+sum(i+1--j) 6 //(2)i不是第一个出场,而是第k个出场,则i+1到k+i-1这段区间第一个出场,k+i到j第k+1个出场 7 //diaoshizhi为dp[i+1][i+k-1] + a[i]*(k-1) + (dp[i+k][j]+k*s

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 ar

uva 10003 Cutting Sticks 简单区间dp

// uva 10003 Cutting Sticks 区间dp // 经典的区间dp // dp(i,j)表示切割小木棍i-j所需要的最小花费 // 则状态转移为dp(i,j) = min{dp(i,k) + dp(k,j) + a[j]-a[i]) // 其中k>i && k<j // a[j] - a[i] 为第一刀切割的代价 // a[0] = 0,a[n+1] = L; // dp数组初始化的时候dp[i][i+1]的值为 0,这表示 // 每一段都已经是切割了的,不

黑书例题 Fight Club 区间DP

题目可以在bnuoj.soj等OJ上找到. 题意: 不超过40个人站成一圈,只能和两边的人对战.给出任意两人对战的输赢,对于每一个人,输出是否可能是最后的胜者. 分析: 首先序列扩展成2倍,破环成链. dp[i][j]表示i和j能够相遇对打,那么dp[i][i+n]为真代表可以成为最后胜者. 枚举中间的k,若i和j都能和k相遇,且i和j至少一人能打赢k,那么i和j可以相遇. 复杂度o(n^3) 1 #include<cstdio> 2 #include<cstring> 3 usi

算法复习——区间dp

感觉对区间dp也不好说些什么直接照搬讲义了2333 例题: 1.引水入城(洛谷1514) 这道题先开始看不出来到底和区间dp有什么卵关系···· 首先肯定是bfs暴力判一判可以覆盖到哪些城市····无解直接输出···有解得话就要想想了···· 这道题关键是要发现··如果一个蓄水池所在城市可以覆盖到一些沙漠城市···那么这些沙漠城市肯定是一段····不然假设有一个城市是断开的而两边都被同一个蓄水池流出的水覆盖,这个城市四周的城市都肯定比它矮···(不理解举个反例吧···反正我举不出来)···然后就