Saleh127

LO 1048

Sep 10th, 2020
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5. ll a[10000],n,k,i,j,l,mx,hi,lo,mi,ans,mid;
  6.  
  7. bool valid(ll x)
  8. {
  9. ll c=0,sum=0;
  10. for(i=0;i<n;i++)
  11. {
  12. if(a[i]>x)
  13. {
  14. return 0;
  15. }
  16. sum+=a[i];
  17. if(sum>x)
  18. {
  19. sum=a[i];
  20. c++;
  21. }
  22. if(i==n-1 && sum<=x) c++;
  23. }
  24.  
  25. if(c<=k) return 1;
  26. return 0;
  27. }
  28.  
  29. int main()
  30. {
  31. ios_base::sync_with_stdio(0);
  32. cin.tie(0);cout.tie(0);
  33.  
  34. test
  35. {
  36. cin>>n>>k;
  37. n++,k++;
  38. mi=100000000,mx=0;
  39. for(i=0;i<n;i++)
  40. {
  41. cin>>a[i];
  42. mi=min(a[i],mi);
  43. mx+=a[i];
  44. }
  45. ans=mi;
  46. lo=mi;
  47. hi=mx;
  48. while(lo<=hi)
  49. {
  50. mid=(lo+hi)/2;
  51. if(valid(mid))
  52. {
  53. ans=mid;
  54. hi=mid-1;
  55. }
  56. else lo=mid+1;
  57. }
  58. cout<<"Case "<<cs<<": "<<ans<<endl;
  59. mx=0;
  60. j=0;
  61. for(i=0;i<n;i++)
  62. {
  63. if(mx+a[i]==ans)
  64. {
  65. cout<<mx+a[i]<<endl;
  66. mx=0;
  67. j++;
  68. }
  69. else if(mx+a[i]>ans)
  70. {
  71. cout<<mx<<endl;
  72. mx=a[i];
  73. j++;
  74. }
  75. else if(n-i==k-j)
  76. {
  77. cout<<mx+a[i]<<endl; ///protidin min hattei hove shei jonno
  78. mx=0; ///check korvo remaining day-n r remaining night-k
  79. j++; /// jodi equal hoi taile print koiraa divoo
  80. }
  81. else mx+=a[i];
  82. }
  83. if(mx>0) cout<<mx<<endl;
  84. }
  85.  
  86.  
  87. return 0;
  88. }
  89.  
  90.  
Advertisement
Add Comment
Please, Sign In to add comment