import java.io.*;
import java.util.*;
class Main{
static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
static int N = 410, INF = 0X3f3f3f3f;
static int[] a = new int[N], sum = new int[N];
static int[][] dpMax = new int[N][N];
static int[][] dpMin = new int[N][N];
public static void main(String[] args) throws Exception{
int n = Integer.valueOf(read.readLine());
String[] ss = read.readLine().split(" ");
for(int i = 1; i <= ss.length; i++){
a[i] = Integer.valueOf(ss[i - 1]);
a[i + n] = Integer.valueOf(ss[i - 1]);
}
for(int i = 1; i <= 2 * n; i++){
sum[i] = sum[i - 1] + a[i];
Arrays.fill(dpMax[i], -INF);
Arrays.fill(dpMin[i], INF);
}
for(int len = 1; len <= n; len++){
for(int i = 1; i + len - 1 <= 2 * n; i++){
int j = i + len - 1;
if(len == 1) {
dpMax[i][j] = dpMin[i][j] = 0;
}else{
for(int k = i; k < j; k++){
dpMax[i][j] = Math.max(dpMax[i][j], dpMax[i][k] + dpMax[k + 1][j] + sum[j] - sum[i - 1]);
dpMin[i][j] = Math.min(dpMin[i][j], dpMin[i][k] + dpMin[k + 1][j] + sum[j] - sum[i - 1]);
}
}
}
}
int max = -INF, min = INF;
for(int left = 1; left <= n; left++){
max = Math.max(max, dpMax[left][left + n - 1]);
min = Math.min(min, dpMin[left][left + n - 1]);
}
System.out.println(min);
System.out.println(max);
}
}
请问为什么枚举中间点(分开点)时,可以取到左端点,但是不可以取到右端点?
大佬求环形的就要拆成2n,原理是什么
环形的话, 首尾可能会相连, 把数组复制成2n, 用来模拟首尾相连的情况。这是一种环形题的常用技巧。
感谢解答,大概知道了