HDU 4283 You Are the One 区间dp

L - You Are the One
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  HDU 4283
Appoint description:  System Crawler  (2016-04-17)

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

区间dp

因为各个参加比赛的男士在小黑屋里满足栈的特性所以枚举每个男士是几个出场的

若 1 2 3 4 5 这时候第一个男士是第3个出场的那么根据栈的特性2,3一定比他先出场

4 5一定比他后出场

那么状态方程就出来了

dp【i,j】=min(dp[i,j],dp[i+1][k]+dp[k+1][j]+ds[i]*(k-i)+(sum[j]-sum[k])(k-i+1)

其中sum为前n项和

ACcode:

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rd3(x,y,z) scanf("%d%d%d,&x,&y,&z)
#define rdl(x) scanf("%I64d,&x);
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define ull unsigned long long
#define maxn 1005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
#define eps 1e-8
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll mul(ll a,ll b,ll p){ll sum=0;for(;b;a=(a+a)%p,b>>=1)if(b&1)sum=(sum+a)%p;return sum;}
inline void Scan(int &x) {
      char c;while((c=getchar())<'0' || c>'9');x=c-'0';
      while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
using namespace std;
int dp[maxn][maxn];
int a[maxn];
int sum[maxn];
int main(){
    int n,loop,cnt=1;
    Scan(loop);
    while(loop--){
        Scan(n);sum[0]=0;
        FOR(i,1,n){Scan(a[i]);sum[i]=a[i]+sum[i-1];}
        FOR(i,0,n)FOR(j,i+1,n)dp[i][j]=INF;
        FOR(l,1,n)for(int i=1;i+l<=n;++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",cnt++,dp[1][n]);
    }
    return 0;
}


你可能感兴趣的:(C++,dp,HDU)