fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. bool fun(int mid,vector<int>&arr,int max_moves){
  4. int sum=0;
  5. for(int i=0;i<arr.size();i++){
  6. sum+=(arr[i]/mid+(arr[i]%mid!=0));
  7. }
  8.  
  9. return sum<=max_moves;
  10.  
  11. }
  12.  
  13. int main()
  14. {
  15. vector<int>arr={4,3,2,7};
  16. int max_moves=5;
  17.  
  18. int l=1; //not l=0 why cause that will cause undefined behavioun in edgecase in fun (divide by 0)/.
  19. int h=*max_element(arr.begin(),arr.end());
  20. int ans=-1;
  21. while(l<=h){
  22. int mid=l+(h-l)/2;
  23. if(fun(mid,arr,max_moves)){
  24. ans=mid;
  25. h=mid-1;
  26. }
  27. else{
  28. l=mid+1;
  29. }
  30.  
  31. }
  32. cout<<ans<<endl;
  33. return 0;
  34. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
4