POJ3273 Monthly Expense(二分+贪心)

题目链接

题意:给N个数,划分为M个块(不得打乱数顺序)。找到一个最好的划分方式,使得块中的最大值最小。

二分的l是N个数的最大值,r是N个数的和。

对于每个mid贪心遍历,看能否满足条件。

#include<iostream>
#include<cmath>
#include<queue>
#include<cstring>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<cstdio>
#include<algorithm>
using namespace std;
int n, m;
int a[100005];

bool judge(int x){
    int pre = 0;
    int cnt = 0;
    for(int i = 1; i <= n; i++){
        if(a[i]-a[pre] > x){
            cnt++;
            pre = i-1;
            i--;
        }else if(i==n){
            cnt++;
        }
    }
    if(cnt <= m)
        return 1;
    else return 0;
}

int main(){
    //freopen("a.txt", "r", stdin);
    while(scanf("%d%d", &n, &m) != EOF){
        int maxx = 0, k;
        a[0] = 0;
        for(int i = 1; i <= n; i++){
            scanf("%d", &k);
            a[i] = a[i-1]+k;
            maxx = max(maxx, k);
        }
        int l = maxx, r = a[n];
        while(l <= r){
            int mid = l+(r-l)/2;
            if(judge(mid))
                r = mid-1;
            else
                l = mid+1;
        }
        cout << l << endl;
    }
    return 0;
}
Zhao Li /
Published under (CC) BY-NC-SA in categories 算法  tagged with 二分  贪心