Article directory
Day17
first question
2021 Demo Tournament
distance and
填空题
public class Main {
public static void main(String[] args) {
String s ="LANQIAO";
int ans = 0;
char[] c = s.toCharArray();
for (int i = 0; i < c.length; i++) {
for (int j = i + 1; j < c.length; j++)
ans += Math.abs(c[j] - c[i]);
}
System.out.println(ans);
}
}
Question 2
The 11th 2020 Blue Bridge Cup National Championship
diffusion
JavaB组第2题
填空题
Multi-source bfs.
import java.util.LinkedList;
import java.util.Queue;
public class Main {
static final int N = 10010;
static boolean[][] st = new boolean[N][N];
public static void main(String[] args) {
bfs();
}
private static void bfs() {
int[] dx= {
0, -1, 0, 1}, dy= {
1, 0, -1, 0};
Queue<PII> q = new LinkedList<>();
// 因为有可能往负半轴扩展 所以我们在原坐标的基础上往右上移2100位 保证所有的点扩展是都在正半轴
q.offer(new PII(0 + 2100, 0 + 2100, 0));
q.offer(new PII(2020 + 2100, 11 + 2100, 0));
q.offer(new PII(11 + 2100, 14 + 2100, 0));
q.offer(new PII(2000 + 2100, 2000 + 2100, 0));
st[0 + 2100][0 + 2100] = true;
st[2020 + 2100][11 + 2100] = true;
st[11 + 2100][14 + 2100] = true;
st[2000 + 2100][2000 + 2100] = true;
int ans = 0;
while (!q.isEmpty()) {
PII t = q.poll();
if (t.cnt == 2021) {
System.out.println(ans);
return;
}
ans++;
for (int i = 0; i < 4; i++) {
int x = t.x + dx[i], y = t.y + dy[i];
if (x < 0 || x >= N || y < 0 || y >= N) continue;
if (st[x][y]) continue;
q.offer(new PII(x, y, t.cnt + 1));
st[x][y] = true;
}
}
}
static class PII{
int x;
int y;
int cnt;
public PII(int x, int y, int cnt) {
this.x = x;
this.y = y;
this.cnt = cnt;
}
}
}
It's timed out, so we can just submit the answer directly.
public class Main {
public static void main(String[] args) {
System.out.println(20312088);
}
}
Question 3
The 4th 2013 Blue Bridge Cup Zhenti
wrong ticket
A simulated question, the solution is in the second question of my previous article: Blue Bridge Cup AcWing Study Notes 4-2 Simulation Learning
Question 4
The 9th 2018 Blue Bridge Cup Provincial Championship
multiples problem
贪心+DP
Refer to the solution of the AcWing Xiaodaidai big brother .
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
static int N = 1010;
static int INF = 0x3f3f3f3f;
static int n,m;
static int[][] f = new int[4][N];
static LinkedList[] a = new LinkedList[N];
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
for (int i = 0; i <= m; i++) a[i] = new LinkedList<Integer>();
for (int i = 1; i <= n; i++) {
int x = sc.nextInt();
a[x % m].add(x);
}
for (int i = 0; i <= 3; i++) Arrays.fill(f[i], -INF);
f[0][0] = 0;
for (int i = 0; i <= m; i++) {
Collections.sort(a[i]);
Collections.reverse(a[i]);
// 选择前3大
for(int u = 0; u < Math.min(3, a[i].size()); u++) {
int x = (int) a[i].get(u);
// 从大到小
for (int j = 3; j >= 1; j--) {
for (int k = 0; k < m; k++)
f[j][k] = Math.max(f[j][k], f[j - 1][mod(k - x, m)] + x);
}
}
}
System.out.println(f[3][0]);
}
private static int mod(int x,int y) {
return (x % y + y) % y;
}
}