第 45 届国际大学生程序设计竞赛(ICPC)亚洲网上区域赛模拟赛 E Eat Walnuts

传送门

E Eat Walnuts

As we all know, in the ACM ICPC held in 2017, the organizer of Xinjiang University presented a box of walnuts to each coach. Our coach is happy to share with the team members except Mr.Watermelon. He is going to test Mr.Watermelon with a game when Mr.Watermelon want to eat some walnuts.

He put some walnuts in a row and let Mr.Watermelon pick one of them. And this walnut is not the first or last in the queue. The price Mr.Watermelon need to pay is : the walnut, the walnut in front of the walnut, and the walnut behind the walnut , the square of the sum of the size of these three walnuts.

For example, now there is a row of walnuts in front of Mr.Watermelon. Their size is: 3 1 50 20 15. If this time Mr.Watermelon picked the third walnut. He needs to pay (1 + 50 + 20) ∗ (1 + 50 + 20) = 5041.

After a walnut is taken away, it will leave the queue. Then Mr.Watermelon picks a walnut again until only two walnuts remain in the queue.

Mr.Watermelon wants to know what the minimum price he will pay when he takes walnuts until there are only two walnuts in the queue. But he needs more time to spend with his girlfriend. So he ask you to help him calculate this problem.

最近这两天刷了,一个区间dp的题和这个题很类似,于是就想到这个题没补。。。。。

当时比赛的时候,没有什么特别的思路,队友想到了dfs,然后加上各种神奇剪枝,结果O(n!)。。。

有个地方得解释一下:比赛的时候以为题目中的左边的核桃,右边的核桃以为是相邻的,因此产生了歧义,实际上左边的核桃,右边的核桃,指的是最左边的核桃,和最右边的核桃。。。

#include <bits/stdc++.h>
#define inf 0x7fffffff
#define ll long long
#define int long long
//#define double long double
#define eps 1e-8
//#define mod 1e9+7
using namespace std;
const int mod=2001;
const int M=2e3+5;
const int N=4*1e2+5;//?????????? 4e8
int f[N][N];
int n,a[N];
int get(int x)
{
	return x*x;
}
void solve()
{
	while(cin>>n)
	{
		memset(f,0x3f,sizeof(f));
		for(int i=0;i<N-1;i++)  f[i][i]=f[i][i+1]=0;
		for(int i=1;i<=n;i++)  scanf("%lld",&a[i]);
		for(int len=3;len<=n;len++)  for(int l=1;l<=n;l++)
		{
			int r=len+l-1;
			if(r>n)  continue;
			for(int k=l+1;k<r;k++)  f[l][r]=min(f[l][r],f[l][k]+f[k][r]+get(a[l]+a[k]+a[r]));
		}
		cout<<f[1][n]<<endl;
	}
	
}
signed main()
{
//	ios::sync_with_stdio(false);
	solve();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值