哪位大佬看看这个二分为什么过不了啊,全wa了
查看原帖
哪位大佬看看这个二分为什么过不了啊,全wa了
1482771
LittleOcean楼主2024/11/27 13:38
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5+10;
int a[N];
int n,k;
bool binary(int x){
    int cut=0,i;
    for(i=0;i<n;){
        int res=0;
        cut++;
        while(res<x){
            res+=a[i];
            i++;
            if(i>=n)break;
        }
    }
    if(cut>=k)return true;
    else return false;
}
int main(){
    int shortest=1e9;
    int r=0;
    scanf("%d %d",&n,&k);
    for(int i=0;i<n;i++) {
        scanf("%d", &a[i]);
        shortest = min(shortest, a[i]);
        r += a[i];
    }
    int l=shortest-1;r=r+1;
    while(l+1!=r){
        int mid=(l+r)/2;
        if(binary(mid))l=mid;
        else r=mid;
    }
    cout<<l;
    return 0;
}
2024/11/27 13:38
加载中...