poj 3273(二分找最小值里面最大的数)最大化最小值

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 ≤ MN) 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.       

先说说题目大意吧:给你一段连续的数(不能更改数据顺序)N个,要求分成M份,得到各自的和,让你设计一个算法,求里面的最大值(是经过程序算出最小的最大值)。

思路:最小化最大值问题,求法是二分法(许多都是这样,不知道为什么),首先是要找到一个区间,即上下界,其中上下界让我wa多次,下面是我wa的代码

 

#include <cstdio>
#include <cmath>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll a[500005];
int n,m;
int ma;
int Sum=0;
bool judge(ll x)
{
   int sum=0;
   int num=0;
   for(int i=0;i<n;i++){
        sum+=a[i];
        if(sum>x)
        {
            num++;
            sum=a[i];
        }
   }
   num++;
   return num>m;
}
void solve()
{
    ll right=100000,left=0,mid;//此处首先上届不能随便取,因为每个数据可能都大于100000,加起来肯定是大于这个上界的
                            //其次是下界不能从0开始,应该从数据里面的最大值开始,想想如果数据是
 7 5
100
400
300
100
500
101
4000  第一次判断就将答案的区域排除了
 

while(right>=left){ mid=(left+right)/2; if(judge(mid)) left=mid+1; else right=mid-1; } cout<<left<<endl;}int main(){ cin>>n>>m; ma=0; for(int i=0;i<n;i++){ scanf("%ld",&a[i]); if(ma<a[i]) ma=a[i]; Sum+=a[i]; } solve(); return 0;}

 

下面是改完的

#include <cstdio>
#include <cmath>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll a[500005];
int n,m;
int ma;
int Sum=0;
bool judge(ll x)
{
   int sum=0;
   int num=0;
   for(int i=0;i<n;i++){
        sum+=a[i];
        if(sum>x)
        {
            num++;
            sum=a[i];
        }
   }
   num++;
   return num>m;
}
void solve()
{
    ll right=Sum,left=0,mid;
    while(right>=left){
        mid=(left+right)/2;
        if(judge(mid))
            left=mid+1;
        else
            right=mid-1;
    }
    cout<<left<<endl;
}
int main()
{
        cin>>n>>m;
        ma=0;
        for(int i=0;i<n;i++){
        scanf("%ld",&a[i]);
        if(ma<a[i])
            ma=a[i];
            Sum+=a[i];
        }
        solve();
    return 0;
}

题解: 
最大化最小值或者最小化最大值的问题可以用二分法来做。为什么用二分? 
以求最大化最小值问题为例。 
我们知道二分有一个前提就是对有序的东西才能二分查找。也就是这个问题变量是有序的才能用二分。 
在求最大化最小值时,既然求,那么这个值一定有一个范围。而求最大化最小值时。既然它可以“最大化”,那我们一般可以推到一个”最大值“。既然它可以“最小值”,那我们一般可以推到一个”最小值“。可知最终的这个“”一定在这个最大值与最小值之间的范围里。当我们在这个范围里挑一个值得时候。我们经过处理,得出一个和题目要求位置相同的变量。我们挑的这个值确定的这个变量状态与题目的要求变量进行比较。这个就是有序的。在本题中就是,但我枚举一个总和k后,求得在这个中和下的最大分段数,这个分段数与题目要求的分m段的比较是有序的。如果比m大,说明段数分多了–>说明k枚举小了,接下来再在大于k的区间枚举。这就可以二分了。

再就是唯一性问题。 
一般而言最大化最小值问题都会只有一个确定的答案(如果有多个答案,也应该都是一样的值)这是显然的。那么在二分过程中我们应该是不断缩小[L,R]的范围,最终使得L==R才退出二分。这样才是一个唯一的结果。如果当我们过程中查到了某个值的状态满足题目要求就退出。我们就无法保证这是唯一的那个答案。

同类题有POJ3258 POJ1905 POJ3122

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值