Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- long long int vessel[1000000+5];
- bool check(long long int n,long long int m,long long int mid)
- {
- long long int i,capacity=mid,sum=0,container=1;
- for(i=0;i<n;i++)
- {
- sum+=vessel[i];
- if(sum>capacity)
- {
- container++;
- sum=vessel[i];
- }
- }
- if(container<=m)
- return true;
- else
- return false;
- }
- int main()
- {
- long long int cas,n,i,m,j,k,sum=0,t;
- cin>>t;
- for(cas=1; cas<=t; cas++)
- {
- cin>>n>>m;
- long long int mx=0;
- for(i=0; i<n; i++)
- {
- cin>>vessel[i];
- sum+=vessel[i];
- mx=max(mx,vessel[i]);
- }
- long long int low=mx,high=sum,mid,res=0;
- while(low<=high)
- {
- mid=(low+high)/2;
- if(check(n,m,mid)==true)
- {
- high=mid-1;
- res=mid;
- }
- else
- low=mid+1;
- }
- cout<<"Case "<<cas<<": "<<res<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement