• HDU 4283 You Are the One 区间dp


    题目链接:

    http://acm.hdu.edu.cn/showproblem.php?pid=4283

    You Are the One

    Time Limit: 2000/1000 MS (Java/Others)
    Memory Limit: 32768/32768 K (Java/Others)
    #### 问题描述 > 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?

    输入

    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)

    输出

    For each test case, output the least summary of unhappiness .

    样例输入

    2

    5
    1
    2
    3
    4
    5

    5
    5
    4
    3
    2
    2

    样例输出

    Case #1: 20
    Case #2: 24

    题意

    n个人排成一排,每个人的生气值是arr[i]*(排在他前面的人数),现在按123...n的顺序进栈,现在要你调整出一种合法的出栈顺序使得所有人的生气值总和最小。

    题解

    区间dp(根据栈的性质)
    对于区间[L,R]我们考虑最前面的人(L),是这个区间里面第K个出栈的,那么就有[L+1,K]个人要先于L出栈,[K+1,R]的人要后于L出栈,这样就吧一个区间分解成两个子区间了。

    代码

    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<ctime>
    #include<vector>
    #include<cstdio>
    #include<string>
    #include<bitset>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<functional>
    using namespace std;
    #define X first
    #define Y second
    #define mkp make_pair
    #define lson (o<<1)
    #define rson ((o<<1)|1)
    #define mid (l+(r-l)/2)
    #define sz() size()
    #define pb(v) push_back(v)
    #define all(o) (o).begin(),(o).end()
    #define clr(a,v) memset(a,v,sizeof(a))
    #define bug(a) cout<<#a<<" = "<<a<<endl
    #define rep(i,a,b) for(int i=a;i<(b);i++)
    #define scf scanf
    #define prf printf
    
    typedef __int64 LL;
    typedef vector<int> VI;
    typedef pair<int,int> PII;
    typedef vector<pair<int,int> > VPII;
    
    const int INF=0x3f3f3f3f;
    const LL INFL=0x3f3f3f3f3f3f3f3fLL;
    const double eps=1e-8;
    const double PI = acos(-1.0);
    
    //start----------------------------------------------------------------------
    
    const int maxn=222;
    
    int dp[maxn][maxn],arr[maxn];
    int sumv[maxn];
    int dfs(int l,int r){
        if(l>=r) return 0;
        if(dp[l][r]>=0) return dp[l][r];
    
        int &res=dp[l][r]=INF;
        for(int i=l;i<=r;i++){
            int fi=dfs(l+1,i);
            int se=dfs(i+1,r)+(sumv[r]-sumv[i])*(i-l+1);
            int cur=arr[l]*(i-l);
            res=min(res,fi+se+cur);
        }
    
        return res;
    }
    
    int n;
    
    int main() {
        int tc,kase=0;
        scf("%d",&tc);
        while(tc--){
            clr(dp,-1);
            scf("%d",&n);
            sumv[0]=0;
            for(int i=1;i<=n;i++){
                scf("%d",&arr[i]);
                sumv[i]=sumv[i-1]+arr[i];
            }
            prf("Case #%d: %d
    ",++kase,dfs(1,n));
        }
        return 0;
    }
    
    //end-----------------------------------------------------------------------
    

    Notes

    当一点头绪都没有的时候,先尝试去固定一些变量,比如这一题,你如果想到先去安排最前面一个人在第k个出来的情况,你就会发现这题可以用区间dp来写。

  • 相关阅读:
    [HDOJ4417]Super Mario(归并树)
    [POJ2104] K-th Number(归并树,二分)
    2017北理校赛G题 人民的名义(FFT)
    [CF762C] Two Strings(预处理,二分答案)
    [CF798D] Mike and distribution(贪心,鸽笼原理,随机)
    [CF798C] Mike and gcd problem(规律,gcd)
    2017北理校赛H题 青蛙过河(线段树, dp, 离散化)
    [CF798B] Mike and strings(暴力)
    [CF798A] Mike and palindrome(水题,trick)
    [CCPC2017]湘潭邀请赛
  • 原文地址:https://www.cnblogs.com/fenice/p/5909544.html
Copyright © 2020-2023  润新知