码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3273

时间:2017-04-11 11:15:37      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:space   http   include   div   org   while   problem   algo   nbsp   

传送门:http://poj.org/problem?id=3273

技术分享

 技术分享

 解题四路:

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

标签:space   http   include   div   org   while   problem   algo   nbsp   

原文地址:http://www.cnblogs.com/IKnowYou0/p/6692530.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!