洛谷:P1063 能量项链(dp,普及+/提高)

题目:

在这里插入图片描述

分析:双倍。然后就是矩阵连乘类似。问题了。

代码:哪错了?放弃了!

#include<bits/stdc++.h>
using namespace std;
int n;
int A[250];
int D[250][250];
int f(int x,int y)
{
 if(D[x][y]!=-1) return D[x][y];
 if(x==y) return 0;
 if(x+1==y) return A[x]*A[y]*A[y+1];
 //if(x+2==y) return A[x]*A[x+1]*A[x+2]+A[x]*A[y]*A[y+1];
 for(int i=x;i<y;i++)
 {
  D[x][y]=max(D[x][y],f(x,i)+f(i+1,y));
 }
 D[x][y]=D[x][y]+A[x]*A[y]*A[y+1];
 return D[x][y];
}
int main()
{
 cin>>n;
 for(int i=0;i<n;i++) 
 {
  int c;
  cin>>c;
  A[i]=A[i+n]=c;
 }
 int maxx=-1;
 for(int i=0;i<=n-1;i++)
 {
  memset(D,-1,sizeof(D));
  maxx=max(maxx,f(i,i+n-1));
 }
 cout<<maxx;
}

猜你喜欢

转载自blog.csdn.net/weixin_42721412/article/details/107630301