前言
整体评价
这相当于蓝桥云课 正式推出算法赛前的公测,主要面向的是大一新生。
比赛题还是以语法题为主,被戏称为"幼稚园杯",^_^。
这场是新生赛的最后一场,有意思的是,这场的rank 1是chatgpt写的。为啥这么说呢?因为代码和注释写得真的太规范了,而且最后一题的时间复杂度超了,只不过那题数据太弱了。这也符合chatgpt当前算法能力的设定。
链接:新生编程排位赛6
A. 妮妮的蓝桥果园2
Q: 求大小为n的数组的累加和
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
for (int i= 0; i < n; i++) {
int v = sc.nextInt();
sum += v;
}
System.out.println(sum);
}
}
B. 阿坤老师的蓝桥魔方
Q: 给定一个a,求其a,a的平方,a的立方的总和?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
System.out.println(a + a * a + a * a * a);
}
}
C. 小依的糖果分配
Q: 总共100名小朋友,前10名每人分得x糖果,后90名每人分得y糖果,总共需要多少个?
10 * x + 90 * y
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int x = sc.nextInt(), y = sc.nextInt();
System.out.println(x * 10 + y * 90);
}
}
}
D. 阿坤老师的独特瓷器
Q: 瓷器有2个属性,高度h和直径d,如果没有一个瓷器,两个属性都严格大于它,那这个瓷器就是独特的,求总共有几个?
这是一道二维偏序的题,好像蓝桥杯特别喜欢二维偏序
思路:优先按某一维度进行排序,然后二级排序根据严格/非严格,灵巧地选择排序顺序。
比如这题,先按高度从大到小排序,对于当前的瓷器, 能严格大于它的一定就是前排的几个,而前排其实只需要维护一个最大直径d即可。
为了回避同高度的瓷器比较,二阶排序按直径从小到大排序。
这种思路,最常见的一道典题:俄罗斯套娃
import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
int n = sc.nextInt();
int[][] px = new int[n][2];
for (int i = 0; i < n; i++) {
px[i][0] = sc.nextInt();
px[i][1] = sc.nextInt();
}
// 先按高度(从大到小),再按直径(从小到大)
Arrays.sort(px, Comparator.comparingInt((int[] a) -> -a[0]).thenComparingInt(a -> a[1]));
int res = 0;
int maxD = 0;
for (int i = 0; i < px.length; i++) {
int d = px[i][1];
if (d >= maxD) {
res++;
}
maxD = Math.max(maxD, d);
}
System.out.println(res);
}
}
E. 阿坤老师的魔方挑战
Q: 给你一个NxN的矩阵,然后整体的上下左右移动,求最大的主对角线和?
这题有些绕,但是有一个特性,因为它是整体的,所以无论怎么移动,矩阵同一斜线上的元素不会变化。
所以就是枚举每一条斜向上的线,求其和,然后找到最大的那个就是。
这样的时间复杂度就是。
import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
int n = sc.nextInt();
int[][] arr = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}
long ans = Long.MIN_VALUE;
for (int i = 0; i < n; i++) {
long tmp = 0;
for (int j = 0; j < n; j++) {
tmp += arr[j][(i + j) % n];
}
ans = Math.max(ans, tmp);
}
System.out.println(ans);
}
}