刷题总结——you are the one(hdu4283)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了刷题总结——you are the one(hdu4283)相关的知识,希望对你有一定的参考价值。

题目:

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····用f[i][j]表示我们在只考虑让i到j个人上场的情况下能获得的最小不高兴值···,然后我们考虑让第i个人在第几个上场··枚举i到j中的k,考虑让第i人再第k个人之后上场··那么第i个人就是第i-k+1个上场的··对答案贡献是(i-k)*num[i],另外k+1到j个人也会对答案有新的贡献,为(i-k+1)*sum[k+1——j]

代码 

  

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=105;
const int inf=0x3f3f3f3f;
int num[N],sum[N],f[N][N],T,n;
inline int R()
{
  char c;int f=0;
  for(c=getchar();c<0||c>9;c=getchar());
  for(;c<=9&&c>=0;c=getchar())  f=(f<<3)+(f<<1)+c-0;
  return f; 
}
int main()
{
 // freopen("a.in","r",stdin);
  T=R();
  for(int t=1;t<=T;t++)
  {
    n=R();
    memset(sum,0,sizeof(sum));
    for(int i=1;i<=n;i++)  num[i]=R(),sum[i]=sum[i-1]+num[i];
    memset(f,0,sizeof(f));
    for(int i=1;i<=n;i++)
      for(int j=i+1;j<=n;j++)  f[i][j]=inf;
    for(int i=n-1;i>=1;i--)
      for(int j=i+1;j<=n;j++)
        for(int k=i;k<=j;k++)
          f[i][j]=min(f[i][j],f[i+1][k]+f[k+1][j]+(k-i+1)*(sum[j]-sum[k])+num[i]*(k-i));
    printf("Case #%d: %d\n",t,f[1][n]);
  }
  return 0;
}

 

以上是关于刷题总结——you are the one(hdu4283)的主要内容,如果未能解决你的问题,请参考以下文章

hdu 4283You Are the One

HDU-4283 You Are the One (区间DP)

HDU4283:You Are the One(区间DP)

HDU 4283---You Are the One(区间DP)

hdu 4283 You Are the One 区间dp

HDU 4283 You Are the One