hdu4283You Are the One【区间dp】


招聘——巴卡斯科技(杭州)、英雄互娱(杭州)

You Are the One

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


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
 

Source
2012 ACM/ICPC Asia Regional Tianjin Online
 

为啥AC率这么高的题还得想这么久==

题意:几个人想上场,之前站一排,有一个堆栈用以调整顺序以致屌丝值*(k-1)总和最小,k是某人的出场顺序。最开始没有纠结在堆栈的记录上真是万幸,然而不幸的是自己固执的认为[l,r]分两段后,最优解是两段的和、当前区间正序、当前区间倒序,这三个中的最小值。这里有一个严重的思维漏洞,堆栈在同一时间内只有一个!而且只能是前一段使用!贴一下错误的代码

/***************
hdu4283
2016.3.10
***************/
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 0x3f3f3f3f
long long num[110],dp[110][110];
long long cal(int l,int r,bool flag)
{
    long long tot=0;
    if(flag)
        for(int i=l;i<=r;i++)
            tot+=(i-1)*num[i];
    else
        for(int i=l;i<=r;i++)
            tot+=(l+r-i-1)*num[i];
    return tot;
}
long long min(long long a,long long b,long long c)
{
    long long tmp=a<b?a:b;
    tmp=tmp<c?tmp:c;
    return tmp;
}
int main()
{
    //freopen("cin.txt","r",stdin);
    int t,n,cas=1;
    scanf("%d",&t);
  //  printf("t=%d",t);
    while(t--)
    {
        scanf("%d",&n);
     //   printf("n=%d",n);
        for(int i=1;i<=n;i++) scanf("%I64d",&num[i]);
      //  for(int i=1;i<=n;i++) printf("%lld",num[i]);
     //   printf("0=%I64d 1=%I64d\n",cal(1,n,0),cal(1,n,1));
        memset(dp,maxn,sizeof(dp));
        for(int i=1;i<=n;i++) dp[i][i]=(i-1)*num[i];
        for(int len=2;len<=n;len++)
        {
            for(int l=1;l+len-1<=n;l++)
            {
                int r=l+len-1;
                for(int k=l;k<r;k++)
                {
                        long long a=cal(l,r,false);
                        long long b=cal(l,r,true);
                        long long tmp=min(a,b,dp[l][k]+dp[k+1][r]);
                        if(tmp<dp[l][r]) dp[l][r]=tmp;
                     //   printf("l=%d k=%d r=%d dp=%I64d\n",l,k,r,dp[l][r]);
                }
            }
        }
        printf("Case #%d: %I64d\n",cas++,dp[1][n]);
    }
    return 0;
}

实际上应该假定[l,r]区间的L是第k个上场(1<=k<=r-l+1)那么这个人贡献的屌丝值就是num[l]*(k-1),既然l是第k个上场,那么在此之前上场的必定是[l+1,l+k-1],剩下的部分需要加两部分dp[l+k][r]+k*(sum[r]-sum[l+k-1])这里好好理解

/***************
hdu4283
2016.3.10
15MS	1772K	1125B	C++
***************/
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 0x3f3f3f3f
int num[110],dp[110][110],sum[110];
int min(int a,int b){if(a<b)return a;return b;}
int main()
{
   // freopen("cin.txt","r",stdin);
    int t,n,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
     //   printf("n=%d",n);
        for(int i=1;i<=n;i++) scanf("%d",&num[i]);
        sum[1]=num[1];
        for(int i=2;i<=n;i++) sum[i]=sum[i-1]+num[i];
        memset(dp,0,sizeof(dp));
        for(int i=1;i<n;i++)
            for(int j=i+1;j<=n;j++)
                dp[i][j]=maxn;
        for(int len=1;len<=n;len++)
        {
            for(int l=1;l+len-1<=n;l++)
            {
                int r=l+len-1;
                for(int k=1;k<=len;k++)
                dp[l][r]=min(dp[l][r],dp[l+1][l+k-1]+dp[l+k][r]+k*(sum[r]-sum[l+k-1])+num[l]*(k-1));
               // printf("l=%d r=%d dp=%d\n",l,r,dp[l][r]);
            }
        }
        printf("Case #%d: %d\n",cas++,dp[1][n]);
    }
    return 0;
}




你可能感兴趣的:(dp)