试题G:青蛙过河——蓝桥杯第十三届省赛Java 大学A组

题目

在这里插入图片描述
在这里插入图片描述

分析

看起来就是贪心+二分+剪枝,由于数据量很大 x<= 10^9,肯定就不是模拟法了,直接贪心。
蛙蛙只需要管每次都跳最大步数的就行。
每次最大步数 1 <= y <= size,所以在这里面进行二分。
剪枝是看需不需要模拟那么多次了,看数据规模肯定是不能模拟,其实无论是正向跳,还是逆向跳结果都一样(能跳就都能跳)。
直接跳满就完事。

代码

package com.maggot.maggotscheduler.bootstrap.utils.lanqiaobei13;


import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;

/**
 * @author huangyichun
 */
public class G青蛙过河 {
    /**
     * 看起来像是一个二分贪心+剪枝的算法。
     * 蛙蛙只需要管每次都跳最大步数的就行
     * 每次最大步数 1 <= y <= size,所以在这里面进行二分
     * 剪枝是看需不需要模拟那么多次了,看数据规模肯定是不能模拟,其实无论是正向跳,还是逆向跳结果都一样(能跳就跳)
     * 直接跳满就完事。
     */
    public static void main(String[] args) {
        System.out.println(getJump(2, 1, 0, 2, 0));
        System.out.println(getJump(2, 1, 0, 1, 0));
        System.out.println(getJump(2, 1, 0, 1, 0, 1, 1));
    }


    public static int getJump(int times, int... h) {
        int max = h.length + 1;
        int min = 0;
        while (min < max) {
            int step = (max + min) / 2;
            if (testStep(times, step, h)) {
                max = step;
            } else {
                min = step + 1;
            }
        }
        return min;
    }

    public static boolean testStep(int times, int step, int[] h) {
        int[] ints = Arrays.copyOf(h, h.length);
        return dp(0, times, step, ints);
    }

    public static boolean dp(int position, int times, int step, int[] ints) {
        Queue<JumpWay> queue = new ArrayDeque<>();
        for (int i = position + step; i >= position && times != 0; i--) {
            if (i >= ints.length) {
                times = 0;
                break;
            }
            int thisTimesJump = Math.min(ints[i], times);
            times -= thisTimesJump;
            ints[i] -= thisTimesJump;
            queue.add(new JumpWay(thisTimesJump, i));
        }
        if (times != 0) {
            return false;
        }

        while (!queue.isEmpty()) {
            JumpWay poll = queue.poll();
            if (!dp(poll.position, poll.times, step, ints)) {
                return false;
            }
        }
        return true;
    }

    static class JumpWay {
        int times = 0;
        int position = 0;

        public JumpWay(int times, int position) {
            this.times = times;
            this.position = position;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值