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

思路:C(d):代表分成M组每组的最大值为d(0<d<sum):二分枚举d;

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=100000;
const int INF=1<<25;
int a[MAXN];
int N,M;
/*flase 代表mid的取值太小
true  代表mid的取值太大
*/
bool cal(int mid){
    int sum=0;
    int cnt=0;
    for(int i=0;i<N;i++){
        if(a[i]>mid)
            return false;
        if(sum+a[i]>mid){
            cnt++;
            sum=0;
        }
        sum+=a[i];
    }
    if(sum>0)
        cnt++;
    if(cnt<=M)
        return true;
    else
        return false;
}
int main(){
    cin>>N>>M;
    int sum=0;
    int Max=-INF;
    for(int i=0;i<N;i++){
        scanf("%d",&a[i]);
        Max=max(Max,a[i]);
        sum+=a[i];
    } 
    if(N==M){
        cout<<Max<<endl;
        return 0;
    }
    if(M==1){
        cout<<sum<<endl;
        return 0;
    }
    int L=0,R=sum;
    while(R-L>1){
        int mid=(L+R)>>1;
        if(cal(mid)){
            R=mid;
        }else{
            L=mid;
        }
    }
    cout<<R<<endl;
    return 0;
}

 

POJ--3273--最大化最小值POJ--3273--最大化最小值 queque_heiyaa 发布了109 篇原创文章 · 获赞 53 · 访问量 3301 私信 关注
上一篇:USACO Optimal Milking


下一篇:傻逼牛的健美操