HDU 3506 Monkey Party————区间dp + 环形dp + 四边形优化

Far away from our world, there is a banana forest. And many lovely monkeys live there. One day, SDH(Song Da Hou), who is the king of banana forest, decides to hold a big party to celebrate Crazy Bananas Day. But the little monkeys don't know each other, so as the king, SDH must do something.
Now there are n monkeys sitting in a circle, and each monkey has a making friends time. Also, each monkey has two neighbor. SDH wants to introduce them to each other, and the rules are:
1.every time, he can only introduce one monkey and one of this monkey's neighbor.
2.if he introduce A and B, then every monkey A already knows will know every monkey B already knows, and the total time for this introducing is the sum of the making friends time of all the monkeys A and B already knows;
3.each little monkey knows himself;
In order to begin the party and eat bananas as soon as possible, SDH want to know the mininal time he needs on introducing.

Input

There is several test cases. In each case, the first line is n(1 ≤ n ≤ 1000), which is the number of monkeys. The next line contains n positive integers(less than 1000), means the making friends time(in order, the first one and the last one are neighbors). The input is end of file.

Output

For each case, you should print a line giving the mininal time SDH needs on introducing.

Sample Input

8
5 2 4 7 6 1 3 9

Sample Output

105

思路:

这道题可以看成是“石子归并”的题,猴子正如石子,交友时间就是石子价值。

石子是围成一圈,所以要用到环形dp,即在1~n个石子后在接1~n-1个石子:

a1, a2, a3, ......, an, a1, a2, ......, a(n-1)

然后进行区间dp,(注意len最多还是只能是n)

由于数据比较大,不优化会导致超时,所以要用四边形优化。

 

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>

#define MAXN 2005
#define INF 0xfffffff
#define ll long long

using namespace std;

ll i, j, k, len, s[MAXN][MAXN];
ll dp[MAXN][MAXN], sum[MAXN];


int main()
{
    ll n;
    while(scanf("%lld", &n) != EOF)
    {
        sum[0] = 0;
        for(i = 0; i <= 2*n; i++)
        {
            for(j = 0; j <= 2*n; j++)
            {
                dp[i][j] = INF;
            }
        }
        for(i = 1; i <= n; i++)
        {
            ll t;
            scanf("%lld", &t);
            s[i][i] = i;
            dp[i][i] = 0;
            sum[i] = sum[i-1] + t;
        }
        for(i = 1; i <= n; i++)
        {
            sum[i+n] = sum[i+n-1] + sum[i];
            dp[i+n][i+n] = 0;
            s[i+n][i+n] = i+n;
        }
        for(len = 2; len <= n; len++)
        {
            for(i = 1; i <= n; i++)
            {
                ll j = i+len-1;
                if(j > 2*n-1) break;
                for(k = s[i][j-1]; k <= s[i+1][j]; k++)
                {
                    if(dp[i][j] > dp[i][k] + dp[k+1][j] + sum[j] - sum[i-1])
                    {
                        dp[i][j] = dp[i][k] + dp[k+1][j] + sum[j] - sum[i-1];
                        s[i][j] = k;
                    }
                }
            }
        }
        ll ans = INF;
        for(i = 1; i <= n; i++)
        {
            ans = min(ans, dp[i][i+n-1]);
        }
        printf("%lld\n", ans);
    }

    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章