Saleh127

LO 1076

Jul 23rd, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define test int t; cin>>t; while (t--)
  4. #define ll long long int
  5. #define fellow_junior ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  6. ll n,m,a[10000],lo,hi;
  7.  
  8. ll solution(ll x)
  9. {
  10. ll c=1,i,s=0;
  11. for(i=0;i<n;i++)
  12. {
  13. if(s+a[i]<=x)
  14. {
  15. s+=a[i];
  16. }
  17. else
  18. {
  19. c++;
  20. s=a[i];
  21. }
  22. }
  23. return c;
  24. }
  25.  
  26. ll srch()
  27. {
  28. ll mid,ans=hi;
  29. while(lo<=hi)
  30. {
  31. mid=(lo+hi)/2;
  32. if(solution(mid)<=m)
  33. {
  34. ans=min(ans,mid);
  35. hi=mid-1;
  36. }
  37. else lo=mid+1;
  38. }
  39. return ans;
  40. }
  41.  
  42. int main()
  43. {
  44. fellow_junior
  45. ll t,i,j,k,l;
  46. cin>>t;
  47. for(i=1;i<=t;i++)
  48. {
  49. cin>>n>>m;
  50. lo=0,hi=0;
  51. for(j=0;j<n;j++)
  52. {
  53. cin>>a[j];
  54. lo=max(lo,a[j]);
  55. hi+=a[j];
  56. }
  57. printf("Case %lld: %lld\n",i,srch());
  58. }
  59.  
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment