Day17
第一题
2021年模拟赛
距离和
填空题
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);
}
}
第二题
第十一届2020年蓝桥杯国赛
扩散
JavaB组第2题
填空题
多源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;
}
}
}
超时了,所以我们直接提交答案即可。
public class Main {
public static void main(String[] args) {
System.out.println(20312088);
}
}
第三题
第四届2013年蓝桥杯真题
错误票据
一道模拟题,题解在我之前这篇文章的第二题:蓝桥杯AcWing学习笔记 4-2模拟的学习
第四题
第九届2018年蓝桥杯省赛
倍数问题
贪心+DP
参考AcWing小呆呆大佬题解。
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;
}
}