ABCD求助
  • 板块学术版
  • 楼主huaji_huaji
  • 当前回复5
  • 已保存回复5
  • 发布时间2023/9/10 13:26
  • 上次更新2023/11/2 21:39:44
查看原帖
ABCD求助
681103
huaji_huaji楼主2023/9/10 13:26
#include <bits/stdc++.h>
#define int __int128
using namespace std;
inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    return x*f;
}
inline void write(int x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
int n,m;
int sum=0;
int a[200005];
inline bool judge(int len){
    int line=1,line_len=0;
    for(int i=1;i<=n;i++){
        if(line_len+a[i]<=len){
            line_len+=a[i]+1;
        }else{
            line++;
            line_len=a[i]+1;
        }
    }
    return line<=m;
}
signed main(){
    n=read();m=read();
    for(int i=1;i<=n;i++){
        a[i]=read();
        sum+=a[i]+1;
    }
    int L=0,R=sum;
    while(L+1<R){
        int M=(L+R)/2;
        if(judge(M))R=M;
        else L=M;
    }
    if(judge(L-1))write(L-1);
    else if(judge(L))write(L);
    else if(judge(L+1))write(L+1);
    else write(L+2);

    return 0;
}
2023/9/10 13:26
加载中...