提交时间:2024-05-19 21:23:44

运行 ID: 47713

#include<stdio.h> #include<stdlib.h> #include<string.h> int check(long *a,long N,long long mid,long M); int main() { long N,M; long *a=NULL,i; long long left=0,right=0,mid=0; int res; scanf("%ld%ld",&N,&M); a=(long*)malloc(N*sizeof(long)); memset(a,0,N); for(i=0;i<N;i++) { scanf("%ld",&a[i]); if(a[i]>left) left=a[i]; right=right+a[i]; } while(left<right) { mid=left+(right-left)/2; res=check(a,N,mid,M); if(res==1) right=mid; else left=mid+1; } printf("%lld\n",left); return 0; } int check(long *a,long N,long long mid,long M) { long count=1,i,temp=0; for(i=0;i<N;i++) { if(temp+a[i]<=mid) temp=temp+a[i]; else if(a[i]<=mid) { count++; temp=a[i]; if(count>M) return -1; } else return -1; } if(count>M) return -1; else if(count<=M) return 1; }