这题就是石子合并那道题的环形形式罢了,那么dp开个2n长度的就表示为环形了(化曲为直思想),只不过是一个石子中有两个数罢了
附上AC代码
import java.util.Scanner;
public class Main {
static int maxn = 400;
static zhuzi[] zhu;//在main中new对象数组
static int[][] dp = new int[maxn][maxn];
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
zhu = new zhuzi[2*N+1];//环状开2n
int[] nums = new int[2*N+1];//环状开2n
//存珠子
for(int i = 1;i <= N;i++){
int t = sc.nextInt();
nums[i+N] = t;
nums[i] = t;
}
for(int i = 1;i <= 2*N - 1;i++){
zhuzi z = new zhuzi(nums[i],nums[i+1]);
zhu[i] = z;
}
zhuzi s = new zhuzi(nums[2*N],nums[1]);
zhu[2*N] = s;
//环形区间DP,化曲为直,一个n环看成2n的直线
for(int len = 1;len <= 2*N;len++){//区间长度为1 - 2n
for(int i = 1;i <= 2*N - len + 1;i++){//区间起点为1 - 2n-len-1
int jmax = i + len - 1;//区间终点 i+len-1
for(int k = i;k < jmax;k++){//选取截断点 i - jmax开
dp[i][jmax] = Math.max(dp[i][jmax],dp[i][k]+dp[k+1][jmax]+zhu[i].getHead()*zhu[k+1].getHead()*zhu[jmax].getTail());
}
}
}
//由于是环状,起点可以任选,所有要列举每个起点到每个终点的dp,取最大值
int ans = 0;
for(int i = 1;i<=N;i++){
ans = Math.max(ans,dp[i][i + N - 1]);//起点是i,终点为i+n-1 !!!
}
System.out.println(ans);
}
}
//珠子类
class zhuzi{
public int head;
public int tail;
public zhuzi(int head, int tail) {
this.head = head;
this.tail = tail;
}
public void setHead(int head) {
this.head = head;
}
public void setTail(int tail) {
this.tail = tail;
}
public int getHead() {
return head;
}
public int getTail() {
return tail;
}
}