最大值最小问题,二分答案+Java代码实现

该博客介绍了如何使用二分查找算法解决一个数组分成k段,每段和最大值最小的问题。文章阐述了问题的解一定存在,并解释了为什么可以采用二分查找策略。通过给出的样例输入和输出,展示了问题的实际应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意简述:长度为n的数组,分成k段,每段的和最大值最小是多少

解题关键:
首先,解一定存在,最大解就是数组的所有元素之和。
其次,如果数组进行划分后,每段之和都不超过m,划分为了t段,那一定可以划分为t-1段。所以我们只需要二分查找能否划分为不超过k段即可,当小于等于k段后,一定可以扩展为k段(因为只需要将某几段拆开就好了)

题目描述
You are given n packages of wi kg from a belt conveyor in order (i=0,1,…n−1). You should load all packages onto k trucks which have the common maximum load PP. Each truck can load consecutive packages (more than or equals to zero) from the belt conveyor unless the total weights of the packages in the sequence does not exceed the maximum load P. Write a program which reads n, k and wi, and reports the minimum value of the maximum load PP to load all packages from the belt conveyor.
输入
In the first line, two integers n and k are given separated by a space character. In the following n lines, wi are given respectively.
输出
Print the minimum value of P in a line.
样例输入
5 3
8
1
7
3
9
样例输出
10
提示
1≤ n≤ 100,000
1≤ k≤ 100,000

Java代码实现

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

public class Main {
   
    public static void</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值