Analysis of the real questions of the 12th Blue Bridge Cup

content

foreword

Exam Skills

Analysis of real questions        

Question 1 (ASC)

Question 2 (cards)

Question 3 (straight line)

Question 4 (Placement of Goods)

Question 5 (Path)

Question 6 (time display)

Question 7 (minimum weight)

Question 8 (Yang Hui's Triangle)

Question 9 (two-way sorting)

Question 10 (Bracket Sequence)

Summarize


foreword

        The 13th provincial competition of the Blue Bridge Cup will start on April 9th. According to the previous practice, there are 4 basic fill-in-the-blank questions and 2-3 programming questions. The province is basically stable.

        The basics of the Blue Bridge Cup provincial exam, except for the last question, are basically simple or medium questions. We don’t need to do all the questions, we just need to ensure that we can do well, then there is basically no problem with saving one. Below is the certificate of merit from the last time I participated in the provincial competition


Exam Skills

        According to my own experience in participating in the competition, the overall difficulty of the provincial competition is relatively low, and most of the questions can be violently scored. For the provincial competition, we need to focus on reviewing the following knowledge points

  1. For the java group, we must be able to master BigInteger and BigDecimal proficiently. If not, learn it immediately. These two classes can realize the operation of large numbers, with miraculous effects! ! !
  2. In the provincial competition, there are basically questions about time every time, so we must also be able to master the time api. In java, such as the Data class, Calendar, LocalDate, LocalDateTime, these classes must also be mastered.
  3. In the provincial competition, the data structure will definitely be tested. The basic DFS and BFS must be learned. The traversal of the tree must be familiar with the characteristics. It is also necessary to learn several basic algorithms for graphs , such as the shortest path.
  4. The above 3 points must be mastered. The following is the improvement category. The Blue Bridge Cup has always been called the Violence Cup in the past. This is because the previous Blue Bridge Cups were indeed violent problem solving, but now the difficulty has been increased. Now , in the last dp (dynamic programming) questions began to increase, if you want to get a good grade, don't hesitate, go to the dynamic programming questions immediately! !
  5. The next thing is related to strings . This is also a lot of tests. It is recommended to go to leetcode to take a look at the string column to get a general understanding of common algorithms. At least there is a direction in the competition.
  6. The next point, full arrangement , I don't know if you will. Since there is no ready-made function in java, it is recommended to write the function yourself now, which is very likely to be used.
  7. This is very important, read the question carefully, read the question carefully, read the question carefully , it is recommended to read the question several times, and then start thinking about the problem-solving idea after you have thoroughly understood the meaning
  8. For difficult problems, if you have no idea for 10 minutes, skip it immediately, do everything you can do first, and then think about it. The difficulty of the Blue Bridge Cup generally increases with the number of questions, that is, the harder you go to the back.
  9. Do the problem, if you are not particularly experienced, it is recommended that the first solution starts with violence, and then optimizes
  10. The last point is that there are very few test cases in the Blue Bridge Cup. After you write the code, you may feel that you have done the right thing and the test case is correct, and then you submit it directly. After the game, you find that you have not considered it carefully. It's too late. After we finish writing the code, we recommend writing at least 5 test cases or more to test the code, and the test cases should be relatively large or tricky.

        In short, the Blue Bridge Cup provincial competition is not difficult. As long as you have learned the basic data structures and algorithms, it is basically no problem to save one. The difference between the provincial competitions is to be careful, and finally repeat it three times, careful, careful, careful


Analysis of real questions        

        The following is an explanation of the topics of the previous session, that is, the twelfth Java Group B


Question 1 (ASC)

        This question will be divided into sub-questions, without explanation, give the answer directly, 76


Question 2 (cards)

         This problem is a violent simulation. We define an array of size 10 to represent 0-9, each value is 2021, and then loop to judge, if the array value is not 0, then continue. We get each digit in turn for the number of the loop, and then subtract it from the array. The description may be a bit vague. Let's look at the code directly.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] nums = new int[10];
        Arrays.fill(nums, 2021);
        int ans = 1;
        //设置标记,是否退出循环
        boolean flag = false;
        while (true) {
            //对于每一个num,通过字符串方式获取每一个数字
            for (char num : (ans + "").toCharArray()) {
                //将对于位置的数字-1,看是否为0
                if (--nums[num - '0'] == 0) {
                    flag = true;
                    break;
                }
            }
            if (flag) break;
            ans++;
        }
        System.out.println(ans);
    }

}

        The answer is: 3181


Question 3 (straight line)

        This question is also simple, and the problem that needs to be paid attention to is the floating point number. y=kx+b, the idea is to calculate the slopes k and b of all straight lines, and then put them into the set to remove the weights. Here, we need to pay attention to the precision problem and the floating point error. I solved it directly by using java's BigDecimal

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashSet;

public class Main {
    public static void main(String[] args) {
        HashSet<String> hashSet = new HashSet<>();
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 21; j++) {
                for (int m = i + 1; m < 20; m++) {
                    for (int n = 0; n < 21; n++) {
                        // y = kx + b通过2个点算出 b和k,然后返回b_k字符串,存入set
                        String bk = getBK(i, j, m, n);
                        hashSet.add(bk);
                    }
                }
            }
        }
        //这里加20是因为有20条直线没有斜率
        System.out.println(hashSet.size() + 20);
    }

    public static String getBK(double x1, double y1, double x2, double y2) {
        // k保留小数点后50位
        BigDecimal k = new BigDecimal(y1 - y2).divide(new BigDecimal(x1 - x2), 50, RoundingMode.FLOOR);
        BigDecimal b = new BigDecimal(y1).subtract(new BigDecimal(x1).multiply(k));
        // b和k都保留15位小数
        b = b.setScale(15, RoundingMode.FLOOR);
        k = k.setScale(15, RoundingMode.FLOOR);
        return b + "_" + k;
    }

}

        The answer is: 40257


Question 4 (Placement of Goods)

        This problem can be solved directly by violence, there is no difficulty, calculate all the factors of n, and then judge in a loop

import java.util.HashSet;

public class Main {
    public static void main(String[] args) {
        long num = 2021041820210418L;
        HashSet<Long> hashSet = new HashSet<>();
        //将num所有因数加入集合
        for (long i = 1; i <= Math.sqrt(num); i++) {
            if (num % i == 0){
                hashSet.add(i);
                hashSet.add(num / i);
            }
        }
        long ans = 0;
        //暴力对所有因数进行循环
        for (Long x : hashSet) {
            for (Long y : hashSet) {
                for (Long z : hashSet) {
                    if (x * y * z == num)ans++;
                }
            }
        }
        System.out.println(ans);
    }

}

         Answer: 2430


Question 5 (Path)

        This question is to investigate the shortest path problem in the graph, but we can convert it into dp. First, find the distance from all points to the next 21, store it in an array, and then perform dp, dp[i] = min (dp[i],dp[ik][k]) k range is 1-21 

public class Main {
    public static void main(String[] args) {
        long[] dp = new long[2022];
        //存放当前点到后面21个点的距离,下标1开始
        long[][] dis = new long[2022][22];
        for (int i = 1; i < 2022; i++) {
            for (int j = 1; j < 22; j++) {
                if (i + j > 2021) break;
                dis[i][j] = getDis(i, i + j);
            }
        }
        for (int i = 1; i < 2022; i++) {
            long min = Long.MAX_VALUE;
            //求出到当前点的最小值
            for (int j = 1; j <= 21; j++) {
                if (i - j <= 0) break;
                min = Math.min(min, dp[i - j] + dis[i - j][j]);
            }
            if (min != Long.MAX_VALUE) dp[i] = min;
        }
        System.out.println(dp[dp.length - 1]);
    }

    public static long getDis(long a, long b) {
        return a * b / gcd(a, b);
    }

    public static long gcd(long a, long b) {
        return a % b == 0 ? b : gcd(b, a % b);
    }
}

        Answer: 10266837


Question 6 (time display)

        For sub-questions, do not explain, give the code directly, pay attention to the output format, it is recommended to use printf

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        long time = new Scanner(System.in).nextLong();
        long h = (time / 1000 / 60 / 60) % 24;
        long m = (time / 1000 / 60) % 60;
        long s = (time / 1000) % 60;
        System.out.printf("%02d:%02d:%02d", h, m, s);
    }
}

Question 7 (minimum weight)

         How do you say this question? It can be dynamically programmed and simulated violently, but the optimal solution is to balance the 3-system simulation. I don’t know if you have heard of the 3-system. Here, we use the 3-system to do it. There are 3 states of weights. It just corresponds, put it on the left, put it on the right, don't put it, the principle is 3 hexadecimal, two or two combinations, -1, 0, 1, after the combination can represent 9 numbers that do not repeat

ternary decimal
-1 -1
0 0
1 1
-1 -1 -4
-1 0 -3
-1 1 -2
1 -1 2
1 0 3
1 1 4

     Ternary numbers can represent all     [-\frac{3^n-1}{2},\frac{3^n-1}{2}](n\in Z)numbers, and because the problem needs to meet the condition that \frac{3^n-1}{2}\geq FEMALE, through calculation, we can find n≥log3​(2N+1), so the weight of the weight we need is 3^{x}, x>=0, the following is It's easy to write code. What I said may not be very clear. You can read the explanation below. If you don't understand, it doesn't matter if you skip it directly. Violence and dp can get most of the points.

举个例子,平衡3进制的1和3,也就是 1 和 10这2个数就可以表示到11,也就是可以表示到4

十进制1,3,9,平衡三进制表示为 1 10 100,
这3个数通过加减运算就可以表示到-1-1-1到111的所有范围,也就是 (-14,14)

十进制1,3,9,27,平衡三进制表示为 1 10 100 1000,
这4个数就可以表示 -1-1-1-1到1111的所有范围也就是(-40,40)

        If you still don't understand, you can refer to  the detailed explanation of symmetric ternary numbers 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        long n = new Scanner(System.in).nextLong();
        int count = 1;
        int i;
        for (i = 1; count < n; i++) {
            count += Math.pow(3, i);
        }
        System.out.println(i);
    }
}

Question 8 (Yang Hui's Triangle)

         This question involves mathematical derivation. Two types of liberation are given below. The first type of violence can get half of the points.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int n = new Scanner(System.in).nextInt();
        if (n == 1) {
            System.out.println(1);
            return;
        }
        //动态dp,2行进行切换
        int[][] dp = new int[2][100000];
        long count = 1;
        dp[0][1] = 1;
        int i = 1, j = 1;
        //当前层数
        for (int layer = 2; ; layer++) {
            //每层的数量和当前层数相同
            for (int k = 0; k < layer; k++) {
                //简单dp
                if (layer % 2 == 0) dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1];
                else dp[i][j] = dp[i + 1][j] + dp[i + 1][j - 1];
                count++;
                if (dp[i][j++] == n) {
                    System.out.println(count);
                    return;
                }
            }
            //根据层数判断是从0开始还是1开始
            i = layer % 2 != 0 ? 1 : 0;
            j = 1;
        }
    }
}

If you want to get the whole part, then you need to carry out mathematical derivation . From the mathematical  point          of view, because the process is more complicated and the space is limited, the link of the derivation is given directly. It is very detailed. You can check it yourself. 


Question 9 (two-way sorting)

         This question also uses mathematics, but we can get 60 points by violent solution, which is also OK. The code of the violent solution and the link of mathematical derivation are given below.

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        Integer[] arr = new Integer[n+1];
        for (int i = 1; i < arr.length; i++) {
            arr[i] = i;
        }
        for (int i = 0; i < m; i++) {
            int num1 = scanner.nextInt();
            int num2 = scanner.nextInt();
            //直接调用api
            if (num1 == 0){
                Arrays.sort(arr, 1, num2+1, (o1, o2) -> o2-o1);
            }else {
                Arrays.sort(arr, num2, n+1);
            }
        }
        for (int i = 1; i <= n; i++) {
            System.out.print(arr[i]+" ");
        }
    }
}

         Since the blog is already very long, the mathematical derivation is not over, just give the link to view  the mathematical derivation by yourself , and you can get 100 points for this


Question 10 (Bracket Sequence)

         The last question is difficult and is generally reserved for those who play ACM. If it is not directly skipped by ACM, it will not have any effect on the score. This question will not be explained. If you are interested, please refer to the  detailed explanation of the bracket sequence . This big guy gave a detailed solution, you can refer to it


Summarize

        The difficulty of the Blue Bridge Cup is mainly concentrated in the following questions. The last question is simply abandoned. If we have no ideas, then we can use violence. How many points can be counted. Everyone has to believe that if you think it is difficult, others will also find it difficult. Blue Bridge Cup provincial competition, 4 fill-in-the-blank questions, 3 programming questions, no problem to save one.

        Finally, let me say that the provincial competition is not difficult. The most important thing is to read the questions carefully, read the questions carefully, read the questions carefully, be careful, be careful, and be careful . I wish you all the best of luck in the Blue Bridge Cup! ! !

Guess you like

Origin blog.csdn.net/m0_51545690/article/details/123979651