蓝桥杯31天冲刺打卡题解(Day12)

Day12

第一题

蓝桥杯算法提高

打水问题

贪心

要想让等待时间最短,就要尽可能的使打水时间少的先分配到水龙头,所以我们可以把时间排序,按照下标依次分配水龙头。

m个人已经占用了m个水龙头,那么第m + 1个人应该该排在第1个水龙头,第m + 2个人应该该排在第2个水龙头,第m + k个人应该该排在第k个水龙头,这样就可以使时间最短。

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

public class Main {

    static final int N = 1010;
    static int[] t = new int[N];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), m = sc.nextInt();
        for (int i = 0; i < n; i++) t[i] = sc.nextInt();
        Arrays.sort(t, 0, n);

        int sum = 0;
        for (int i = m; i < n; i++) { // i从m开始枚举 小于n 分配水龙头
            int j = i;
            while (j - m >= 0) {
                sum += t[j - m];
                j -= m;
            }
        }
        System.out.println(sum);
    }
}

第二题

蓝桥杯算法提高

夺宝奇兵

递推

这道题就是数字三角形。

解决这道问题的方法有很多,动态规划,枚举,但是我们最简单的方式就是用递推式求解。

image-20220319210238650

import java.util.Scanner;

public class Main {

    static final int N = 110;
    static int[][] f = new int[N][N];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= i; j++)
                f[i][j] = sc.nextInt();

        for (int i = n - 1; i >= 1; i--) {
            for (int j = 1; j <= n; j++) {
                f[i][j] += Math.max(f[i + 1][j], f[i + 1][j + 1]); // 是它正下方的值大还是右下方的值大
            }
        }

        System.out.println(f[1][1]); // 最后爬到顶端就是我们的最大值
    }
}

第三题

第九届2018年蓝桥杯国赛

调手表

C++B组第4题

bfs

广搜出来的每一个时间点的步数都是最小步数,最后遍历一遍答案即可。

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {

    static final int N = 100010;
    static int[] a = new int[N];
    static int n, k;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        k = sc.nextInt();

        bfs();

        int ans = 0;
        for (int i = 1; i < n; i++) ans = Math.max(ans, a[i]);

        System.out.println(ans);
    }

    private static void bfs() {
        Queue<PII> q = new LinkedList<>();
        q.offer(new PII(0, 0));

        while (!q.isEmpty()) {
            PII t = q.poll();
            int x = t.x, y = t.y; // x是时间 y是步数
            if (x + 1 < n && a[x + 1] == 0) {
                a[x + 1] = y + 1;
                q.offer(new PII(x + 1, y + 1));
            }
            if (a[(x + k) % n] == 0) {
                a[(x + k) % n] = y + 1;
                q.offer(new PII((x + k) % n, y + 1));
            }
        }
    }

    static class PII {
        int x;
        int y;

        public PII(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
}
  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小成同学_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值