悬赏两个关注,急!!!
查看原帖
悬赏两个关注,急!!!
649111
封禁用户楼主2022/8/15 10:28

这道题我是用了二分答案

想先输出一下抄写页数最多的人用去的时间

可是......

输入 #1

9 3
1 2 3 4 5 6 7 8 9

期望输出:17

实际输出:45

#include <bits/stdc++.h>
using namespace std;
int a,b,c,d,n[10000],low,maxx,mid,ls;
bool ch(int mid){
    ls=1;d=0;
    for(int i=a;i>=1;i--){
        if(d+n[i]>mid){
            d=0;
            ls++;
        }
        d+=n[i];
    }
    return ls<=b;
}
int main() {
    cin>>a>>b;
    for(int i=1;i<=a;i++){
        cin>>n[i];
        low=max(low,n[i]);
        maxx+=n[i];
    }
    low=1;
    while(low<maxx){
        mid=(low+maxx+1)/2;
        if(ch(mid)){
            low=mid;
        }
        else{
            maxx=mid-1;
        }
    }
    cout<<maxx;
}
2022/8/15 10:28
加载中...