洛谷 P1063 能量项链
题目传送门
解题思路:
一道很模板的环形dp,要注意状态转移时串的头和尾到底是哪个.
AC代码:
1 #include<cstdio> 2 #include<iostream> 3 4 using namespace std; 5 6 int n,a[202],f[202][202],ans; 7 8 int main() 9 { 10 scanf("%d",&n); 11 for(int i = 1;i <= n; i++){ 12 scanf("%d",&a[i]); 13 a[i+n] = a[i]; 14 } 15 for(int p = 1;p < n; p++) 16 for(int i = 1,j = i + p;i < n + n && j <= n + n; i++,j = i + p) 17 for(int k = i;k < j; k++) 18 f[i][j] = max(f[i][j],f[i][k] + f[k+1][j] + a[i] * a[k+1] * a[j+1]); 19 for(int i = 1;i <= n; i++) 20 ans = max(ans,f[i][i+n-1]); 21 printf("%d",ans); 22 return 0; 23 }