2 条题解
-
3
#include<bits/stdc++.h> #define int long long using namespace std; int a[300001]; int n,m; bool check(int d){ int s=0,o=0; for(int i=1;i<=n;i++){ s+=a[i]+1; if(s+a[i+1]>d){ s=0; o++; } } if(o>m){ return false; }else{ return true; } } signed main(){ cin>>n>>m; int l=-1,r=0; for(int i=1;i<=n;i++){ cin>>a[i]; r+=a[i]; l=max(l,a[i]); } a[n+1]=1e18; int p=0; while(l<=r){ int t=(l+r)/2; if(check(t)==false){ l=t+1; }else{ r=t-1; p=t; } } cout<<p; return 0; } -
0
#include<bits/stdc++.h> using namespace std; long long n,m,a[200005],l,r,mid; int main(){ scanf("%lld%lld",&n,&m); l=0; r=0; for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); l=max(l,a[i]); r+=a[i]+1; } while(l<r){ mid=(l+r)/2; long long sum=0,t=1; for(int j=1;j<=n;j++){ if(sum==0) sum=a[j]; else if(sum+1+a[j]<=mid) sum+=1+a[j]; else t++,sum=a[j]; } if(t<=m)r=mid; else l=mid+1; } cout<<l; return 0; }
- 1
信息
- ID
- 4959
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 2
- 标签
- 递交数
- 16
- 已通过
- 8
- 上传者