最大化最小值问题
描述
有一类常见问题叫做最小值最大化或者最大值最小化。这类问题一般是用二分搜索来解决。
首先二分搜索解决的问题必须具备单调性这个性质,这是使用二分搜索的必要条件,我们分析两个问题。
1.最小值最大化:我们假设x为最大的最小值,那么x-1是满足条件的,但他并不满足最大,x+1是不满足条件的,假设我们左边界是L,右边界是R,我们二分一个答案ans,ans为最后一个满足条件的数,我们是不是可以类比二分搜索(一)中的last_less_equal()或者last_less()这个问题和这两者是差不多的。
2.最大值最小化:我们假设x为最小的最大值,那么x-1是不满足条件的,x+1是满足条件的,但他不满足最小,假设我们左边界是L,右边界是R,我们二分一个答案ans,ans为第一个满足条件的数,我们是不是可以类比二分搜索(一)中的lower_bound()或者upper_bound()这个问题和这两者是差不多的。
所以综上所述并根据我在二分搜索(一)——各种二分中的描述:最小值最大化的二分区间是右闭左开(L,R],每次二分的中心为M=(L+R+1)/2;最大值最小化的二分区间是左闭右开,[L,R),每次二分的中心为M=(L+R)/2。
相关题目
分享巧克力
你有一大块巧克力,它由一些甜度不完全相同的小块组成。我们用数组 sweetness 来表示每一小块的甜度。
你打算和 K 名朋友一起分享这块巧克力,所以你需要将切割 K 次才能得到 K+1 块,每一块都由一些 连续 的小块组成。
为了表现出你的慷慨,你将会吃掉 总甜度最小 的一块,并将其余几块分给你的朋友们。
请找出一个最佳的切割策略,使得你所分得的巧克力 总甜度最大,并返回这个 最大总甜度。
Assemble
Recently your team noticed that the computer you use to practice for programming contests is not
good enough anymore. Therefore, you decide to buy a new computer.
To make the ideal computer for your needs, you decide to buy separate components and assemble
the computer yourself. You need to buy exactly one of each type of component.
The problem is which components to buy. As you all know, the quality of a computer is equal to
the quality of its weakest component. Therefore, you want to maximize the quality of the component
with the lowest quality, while not exceeding your budget.
Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:
• One line with two integers: 1 ≤ n ≤ 1000, the number of available components and 1 ≤ b ≤
1000000000, your budget.
• n lines in the following format: ‘type name price quality’, where type is a string with the type
of the component, name is a string with the unique name of the component, price is an integer
(0 ≤ price ≤ 1000000) which represents the price of the component and quality is an integer
(0 ≤ quality ≤ 1000000000) which represents the quality of the component (higher is better). The
strings contain only letters, digits and underscores and have a maximal length of 20 characters.
It will always possible to construct a computer with your budget.
Output
Per testcase:
• One line with one integer: the maximal possible quality.
Sample Input
1
18 800
processor 3500_MHz 66 5
processor 4200_MHz 103 7
processor 5000_MHz 156 9
processor 6000_MHz 219 12
memory 1_GB 35 3
memory 2_GB 88 6
memory 4_GB 170 12
mainbord all_onboard 52 10
harddisk 250_GB 54 10
harddisk 500_FB 99 12
casing midi 36 10
monitor 17_inch 157 5
monitor 19_inch 175 7
monitor 20_inch 210 9
monitor 22_inch 293 12
mouse cordless_optical 18 12
mouse microsoft 30 9
keyboard office 4 10
Sample Output
9
你有b块钱,想要组装一台电脑。给出n个配件格子的种类,品质因子和价格,要求每种类型的配件各买一个,总价格不超过b,且品质最差的配件的品质因子尽量大。
Aggressive cows
总时间限制: 1000ms 内存限制: 65536kB
描述
Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,…,xN (0 <= xi <= 1,000,000,000).
His C (2 <= C <= N) cows don’t like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?
输入
Line 1: Two space-separated integers: N and C
Lines 2..N+1: Line i+1 contains an integer stall location, xi
输出- Line 1: One integer: the largest minimum distance
样例输入
5 3
1
2
8
4
9
样例输出
3
提示
OUTPUT DETAILS:
FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.
Huge input data,scanf is recommended.
来源
USACO 2005 February Gold
农民约翰有用C只牛,然后他有N个隔间,每个隔间都有自己的坐标位置(一维的)pos,如何安排把牛安排进隔间才能使,所有牛之间距离的最小值最大,我们不需要求这个分配方案,我们只需要求这个最小距离的最大值,很裸的最小值最大化。
Monthly Expense
Monthly Expense
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 42212 Accepted: 15462
Description
Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over the next N (1 ≤ N ≤ 100,000) days.
FJ wants to create a budget for a sequential set of exactly M (1 ≤ M ≤ N) fiscal periods called "fajomonths". Each of these fajomonths contains a set of 1 or more consecutive days. Every day is contained in exactly one fajomonth.
FJ's goal is to arrange the fajomonths so as to minimize the expenses of the fajomonth with the highest spending and thus determine his monthly spending limit.
Input
Line 1: Two space-separated integers: N and M
Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Line 1: The smallest possible monthly limit Farmer John can afford to live with.
Sample Input
7 5
100
400
300
100
500
101
400
Sample Output
500
Hint
If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.
Source
USACO 2007 March Silver
共n个月,给出每个月的开销.将n个月划分成m个时间段,求m个时间段中开销最大的时间段的最小开销值。