• 蓝桥杯算法赛 第 6 场 小白入门赛 解题报告 | 珂学家 | 简单场 + 元宵节日快乐



    前言

    在这里插入图片描述


    整体评价

    因为适逢元宵节,所以这场以娱乐为主。


    A. 元宵节快乐

    题型: 签到

    节日快乐,出题人也说出来自己的心愿, 祝大家AK快乐!

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            System.out.println("Today AK!");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    B. 猜灯谜

    思路: 模拟

    按题意模拟即可,环状结构

    import java.io.BufferedInputStream;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    import java.util.stream.Collectors;
    
    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];
            for (int i = 0; i < n; i++) {
                arr[i] = sc.nextInt();
            }
            List<Integer> res = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                res.add(arr[(i - 1 + n) % n] + arr[(i + 1) % n]);
            }
            System.out.println(res.stream().map(String::valueOf).collect(Collectors.joining(" ")));
        }
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    C. 数学奇才

    思路: 思维题

    可以贪心逆序从右到左翻转,每次翻转保证最右的非负数多一项,题意保证最多有n次。

    所以,必然存在操作序列,使得数组元素全变非负形态。

    ∑ i = 0 i = n − 1 a b s ( a i ) \sum_{i=0}^{i=n-1} abs(a_i) i=0i=n1abs(ai)

    import java.io.BufferedInputStream;
    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();
            long[] arr = new long[n];
    
            long sum = 0;
            for (int i = 0; i < n; i++) {
                arr[i] = sc.nextLong();
                sum += Math.abs(arr[i]);
            }
            System.out.println(sum);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    D. 你不干?有的是帕鲁干

    思路: 解方程

    假设第一个元素为y, 那么

    ( y + 2 ) 2 − y 2 = 4 ( y + 1 ) = x (y + 2) ^ 2 - y ^ 2 = 4(y + 1) = x (y+2)2y2=4(y+1)=x

    那么该y为

    y = x / 4 − 1 y=x/4 - 1 y=x/41

    这边需要保证x是4的倍数,同时y必须为>=1的奇数

    import java.io.BufferedInputStream;
    import java.util.Scanner;
    
    public class Main {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(new BufferedInputStream(System.in));
    
            int t = sc.nextInt();
            while (t-- > 0) {
                long x = sc.nextLong();
                if (x % 4 != 0) {
                    System.out.println("No");
                } else {
                    long y = x / 4 - 1;
                    if (y <= 0 || y % 2 == 0) {
                        System.out.println("No");
                    } else {
                        System.out.println("Yes");
                        System.out.println((y) + " " + (y + 2));
                    }
                }
            }
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    E. 等腰三角形

    思路: 贪心+双指针

    感觉这题是这场周赛最难的,但是和历史比,算中等偏下的题

    因为三角形中,边的关系需要满足,

    任意两边之和必须大于第三边 任意两边之和必须大于第三边 任意两边之和必须大于第三边

    这题也是最大二分匹配模型

    不过这题有个取巧的地方,就是

    从底边出发

    如果 b i < b j b_i < b_j bi<bj, 则选用 b i b_i bi b j b_j bj 的结果不会变差

    因此可以对腰边和底边进行排序

    然后使用双指针,进行贪心配对

    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];
            int[] brr = new int[n];
    
            for (int i = 0; i < n; i++) {
                arr[i] = sc.nextInt();
            }
            for (int i = 0; i < n; i++) {
                brr[i] = sc.nextInt();
            }
            Arrays.sort(arr);
            Arrays.sort(brr);
            // 双指针
            int res = 0;
            int j = 0;
            for (int i = 0; i < n; i++) {
                while (j < n && arr[j] * 2 <= brr[i]) {
                    j++;
                }
                if (j < n) {
                    j++;
                    res++;
                }
            }
            System.out.println(res);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    F. 计算方程

    思路: 二分

    因为其函数是单调的,呈现单调性

    因此用二分是最优解

    import java.io.BufferedInputStream;
    import java.util.Scanner;
    
    public class Main {
    
        static boolean check(int x, int k, int m) {
            double r1 = Math.sqrt(1.0 * x);
            double r2 = (int)(Math.log(x) / Math.log(k));
            return r1 + r2 > m;
        }
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(new BufferedInputStream(System.in));
            int t = sc.nextInt();
            while (t-- > 0) {
                int k = sc.nextInt(), m = sc.nextInt();
                int l = 1, r = m * m;
                while (l <= r) {
                    int mid = l + (r - l) / 2;
                    if (check(mid, k, m)) {
                        r = mid - 1;
                    } else {
                        l = mid + 1;
                    }
                }
                System.out.println(l);
            }
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    写在最后

  • 相关阅读:
    计算机硬件基本组成和各硬件工作原理
    dolphinscheduler任务莫名重跑
    命令执行相关函数及各类命令执行绕过技巧
    Vue2 分页
    物联网系统
    Ansible playbook中 block用法
    【Python】Python 将一个文件夹备份到一个 ZIP 文件
    【矩阵论】1.准备知识(下)
    ADO.NET之SqlDataAdpter对象
    电脑使用技巧
  • 原文地址:https://blog.csdn.net/m0_66102593/article/details/136288986