#include<bits/stdc++.h> using namespace std; #define ll long long int int isval(ll a[],ll n,ll k,ll m) { ll std=1,cp=0; for(int i=0;i<n;i++) { if(cp+a[i]<m) cp+=a[i]; else { std++; //cp=0; if(std>k) return 0; } } return 1; } ll bs(ll a[],ll n,ll k) { ll su=0; for(ll i=0;i<n;i++) su+=a[i]; ll s=0;ll e=su,m=0; //ll ans=a[n-1]; while(s<=e) { m=floor((s+e)/2); if(isval(a,n,k,m)) e=m-1; else s=m+1; } return m; } int main() { ll t,n; cin>>t; while(t–) { ll k; cin>>n>>k; ll a[n]; for(ll i=0;i<n;i++) cin>>a[i]; cout<<bs(a,n,k)<<"\n"; } return 0; }
Wrong answer for book allocation problem
Plz send your code by saving on ide
There are few errors in your code, plz correct them,
firstly, in line no 31, no need to use ceil value for ,calculating middle value and dnt initialise it with 0
maintain a variable long long int finalans=0; in case of your ll bs function, since your updated value is not getting stored, so store it also. try making these changes in your code
Sorry I didn’t get u I made some changes my code gives 112 instead of 113 for:
1
4 2
12 34 67 90
Could u please clarify?
I have modified your code, try to submit it now…
Yeah got it.Thank you very much.