# 2.6-Java提高组 蓝桥算法训练营

Feishi11 2023-02-06 20:20:44

# 石子合并：区间dp模板题

``````

import java.util.Scanner;

public class Main{
static final int N = 210;
static final int INF = Integer.MAX_VALUE;

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int nn = sc.nextInt();
int n = nn << 1;
int[] a = new int[n + 1];
int[][] dpmin = new int[N][N];
int[][] dpmax = new int[N][N];
//因为题目说是环形的所以在原来的模板上改成链形的
for (int i = 1; i <= nn; i++) {
a[i] = sc.nextInt();
a[i + nn] = a[i];
}
for (int i = 1; i <= n; i++) {
a[i] += a[i - 1];
}
for (int i = 1; i < nn; i++) {
for (int j = 1; j <= n - i; j++) {
int mid = i + j;
dpmin[j][mid] = INF;
for (int k = j; k < mid; k++) {
int sum = a[mid] - a[j - 1];
dpmin[j][mid] = Math.min(dpmin[j][mid], dpmin[j][k] + dpmin[k + 1][mid] + sum);
dpmax[j][mid] = Math.max(dpmax[j][mid], dpmax[j][k] + dpmax[k + 1][mid] + sum);
}
}
}
int max = -INF, min = INF;
for (int i = 1; i <= nn; i++) {
max = Math.max(max, dpmax[i][i + nn - 1]);
min = Math.min(min, dpmin[i][i + nn - 1]);
}
System.out.println(min + "\n" + max);
}
}
``````

# 三步必杀：差分，前缀和

``````//计算完两次差分两次前缀和求出xor max
import java.io.*;

public class Main {
static int N = (int) (1e7 + 10);
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(ss[0]);
int m = Integer.parseInt(ss[1]);
long[] c = new long[N];
for (int i = 1; i <= m; i++) {
int l = change(ss[0]), r = change(ss[1]), s = change(ss[2]), e = change(ss[3]);
int C = (e - s) / (r - l);
c[l] += s;
c[l + 1] += C - s;
c[r + 1] -= C + e;
c[r + 2] += e;
}
long[] b = new long[N];
long max = 0, xor = 0;
for (int i = 1; i <= n; i++) b[i] = b[i - 1] + c[i];
for (int i = 1; i <= n; i++) {
b[i] += b[i - 1];
xor ^= b[i];
max = Math.max(b[i], max);
}
out.println(xor + " " + max);
out.flush();
br.close();
out.close();
}

static int change(String s) {
return Integer.parseInt(s);
}
}
``````

...全文
32 回复 打赏 收藏 举报

4.2w+

2023-02-06 20:20

刷题！