2 solutions
-
1
凡是二分题,难点总是在找check,所以只要能找到check,题就很容易了
#include<iostream> using namespace std; #define MAX 1000000007 #define N 100007 int n,m,s; int a[N]; int check(int x){ int now=0,cns=1; for(int i=1;i<=n;i++){ if(now+a[i]<=x)now+=a[i]; else { cns++; i--; if(cns>m)return false; now=0; } } return true; } int search(int l,int r){ int mid; while(l<=r){ mid=(l+r+1)>>1; if(check(mid))r=mid-1; else l=mid+1; } return r+1; } int main(){ ios::sync_with_stdio(false); cin>>n>>m; for(int i=1;i<=n;i++){ cin>>a[i]; s+=a[i]; } int ans=search(0,s); cout<<ans; return 0; }
-
0
打卡
#include <bits/stdc++.h> using namespace std; int w[100007]; int main(){ int n,m,right=0,left=0; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ scanf("%d",&w[i]); right+=w[i]; left=max(left,w[i]); } int mm=right; while(left<=right){ int mid=(left+right)>>1; int ans=1; int sum=0; for(int i=1;i<=n;i++){ if(sum+w[i]>mid){ ans++; sum=w[i]; } else sum+=w[i]; } if(ans<=m) right=mid-1; else left=mid+1; } cout<<right+1; return 0; }
- 1
Information
- ID
- 168
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 8
- Tags
- # Submissions
- 113
- Accepted
- 17
- Uploaded By