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


前言


整体评价

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


A. 元宵节快乐

题型: 签到

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

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        System.out.println("Today AK!");
    }
}

B. 猜灯谜

思路: 模拟

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

java 复制代码
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(" ")));
    }
    
}

C. 数学奇才

思路: 思维题

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

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

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

java 复制代码
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);
    }

}

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

思路: 解方程

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

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

那么该y为

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

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

java 复制代码
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));
                }
            }
        }
    }

}

E. 等腰三角形

思路: 贪心+双指针

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

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

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

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

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

从底边出发

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

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

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

java 复制代码
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);
    }

}

F. 计算方程

思路: 二分

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

因此用二分是最优解

java 复制代码
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);
        }
    }

}

写在最后

相关推荐
杨了个杨89826 分钟前
Keepalived + Nginx + HAProxy 高可用架构部署实战案例
java·nginx·架构
kaikaile199511 分钟前
数字全息图处理系统(C# 实现)
开发语言·c#
8Qi81 小时前
LeetCode 516:最长回文子序列
算法·leetcode·职场和发展·动态规划
秋91 小时前
Go语言(Golang)开发工程师全景解析:岗位职责·语言优势与使用场景·各城市薪资·发展前景·高考志愿填报(2026版)
开发语言·golang·高考
huangdong_2 小时前
1688商品图片采集技术解析:登录态处理与SKU图自动分类
开发语言
马士兵教育2 小时前
Java还有前景吗?Java+AI大模型学习路线及项目?
java·人工智能·python·学习·机器学习
youngerwang2 小时前
【从搬运工到协处理器:网卡芯片架构、算法、验证与边缘演进深度剖析】
网络·算法·架构·芯片
chase_my_dream2 小时前
C++ + SLAM 高频面试问题整理
开发语言·c++·面试
snow@li3 小时前
Java:理解 Gradle / 后端项目的管家 / 打包SpringBoot 应用 / 完成编译、下载依赖、运行测试、打包 JAR/WAR / 速查表
java
KaMeidebaby3 小时前
卡梅德生物技术快报|纯化重组蛋白实操详解
人工智能·python·tcp/ip·算法·机器学习