E Eat Walnuts
As we all know, in the ACM ICPC held in 2017, the organizer of * 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;
}