Monkey Party HDU 3506(区间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

中文概要:

一群猴子围成一圈,每个猴子有一个权值,然后每次选相邻的两个猴子交友,代价就是两个权值和,a和b交友之后呢,a的朋友和b的朋友就是朋友了,代价是a,b的朋友的总和,让你求最小代价

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF_LL = 9223372036854775807LL;
const int maxn = 2e3 + 10;
ll pre[maxn];//储存权值 
ll dp[maxn][maxn];
ll s[maxn][maxn];
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF) 
	{
		memset(dp,0,sizeof(dp));
		memset(s,0,sizeof(s));
		for(int i=1;i<=n;i++)
		{
			scanf("%lld",&pre[i]);
			pre[i]+=pre[i-1];
		}
		for(int i=n+1;i<=n+n;i++)
		{
			pre[i]=pre[n]+pre[i-n];
		}
		for(int i=1;i<=n+n;i++)
		{
			dp[i][i]=0;
			s[i][i]=i;
		}
	    for(int l=2;l<=n;l++)
	    {
	    	for(int i=1;i<=2*n-l+1;i++)
	    	{
	    		int j=i+l-1;
	    		dp[i][j]=INF_LL;
	    		for(int k=s[i][j-1];k<=s[i+1][j];k++)
	    		{
	    			if(dp[i][j]>dp[i][k]+dp[k+1][j])
	    			{
	    				dp[i][j]=dp[i][k]+dp[k+1][j];
	    				s[i][j]=k;
					}
				}
				dp[i][j]+=pre[j]-pre[i-1];
			}
		}
		ll res = INF_LL;
        for (int i = 1; i <= n; i++) {
            res = min(res,dp[i][i + n - 1]);
	}
	printf("%lld\n",res);
}
	return 0;
}

思路:

定义 s(i,j) 表示 m(i,j) 取得最优值时对应的下标(即 i≤k≤j 时,k 处的 w 值最大,则 s(i,j)=k

这个和经典的取石子问题及其相似,不同的是,这个可以围成一个圈,我们只要预处理下 n*2即可,然后用四边形不等式优化下

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

deebcjrb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值