hdu3506 Monkey Party

本文介绍了一个经典的猴子派对问题,该问题需要通过动态规划解决,特别是应用了四边形不等式优化技巧来减少计算复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Monkey Party

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 590    Accepted Submission(s): 255


Problem Description
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
 

Author
PerfectCai
 

Source
 

Recommend
zhengfeng



这题的状态转移方程很显然,但是需要用到四边形不等式优化。

很让人蛋疼的是四边形不等式优化的dp好像还没有办法写成递归形式,不知道哪位大神知道,请赐教。。。

代码

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

#define INF 999999999

int a[2005];
int s[2005][2005];
int dp[2005][2005];
int w[2005];

int main()
{
    int i,j,n,k,t,tag,ans;
    while(scanf("%d",&n)!=EOF)
    {
        memset(dp,-1,sizeof(dp));
        for (i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        for (i=0;i<n;i++)
        {
            a[i+n]=a[i];
        }
        for (i=0;i<2*n-1;i++)
        {
            dp[i][i]=0;
            s[i][i]=i;
        }
        for (i=0;i<2*n-1;i++)
        {
            if (i==0) w[i]=a[i];
            else w[i]=a[i]+w[i-1];
        }
        for (k=1;k<=n;k++)
        {
            for (i=0;i<2*n-k-1;i++)
            {
                j=k+i;
                for (t=s[i][j-1];t<=s[i+1][j];t++)
                {
                    if (i>0) tag=w[j]-w[i-1];
                    else tag=w[j];
                    if (dp[i][j]==-1 || dp[i][j]>dp[i][t]+dp[t+1][j]+tag)
                    {
                        dp[i][j]=dp[i][t]+dp[t+1][j]+tag;
                        s[i][j]=t;
                    }
                }
            }
        }
        ans=INF;
        for (i=0;i<n;i++)
        {
            ans=min(ans,dp[i][i+n-1]);
        }
        printf("%d\n",ans);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值