Advertisement
Saleh127

UVA 714

Apr 14th, 2021
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 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. bool v[1000];
  6. ll a[20000],n,k;
  7.  
  8. ll check(ll b)
  9. {
  10. memset(v,0,sizeof v);
  11. ll c=0,sum;
  12. for(ll i=n-1;i>=0;)
  13. {
  14. sum=0;
  15. while(i>=0 && sum+a[i]<=b)
  16. {
  17. sum+=a[i];
  18. i--;
  19. }
  20. if(sum==0) return k+1;
  21. c++;
  22. v[i]=1;
  23. }
  24. return c;
  25. }
  26.  
  27. int main()
  28. {
  29. ios_base::sync_with_stdio(0);
  30. cin.tie(0);cout.tie(0);
  31.  
  32. test
  33. {
  34. ll lo=0,hi=0,i,j,l,c=0,mid;
  35. cin>>n>>k;
  36. for(i=0;i<n;i++)
  37. {
  38. cin>>a[i];
  39. lo=max(lo,a[i]);
  40. hi+=a[i];
  41. }
  42.  
  43. while(lo<hi)
  44. {
  45. mid=(lo+hi)/2;
  46. if(check(mid)<=k)
  47. {
  48. hi=mid;
  49. }
  50. else lo=mid+1;
  51. }
  52.  
  53. c=check(hi);
  54. for(i=0;i<n && c<k;i++)
  55. {
  56. if(v[i]==0)
  57. {
  58. v[i]=1;
  59. c++;
  60. }
  61. }
  62. for(i=0;i<n;i++)
  63. {
  64. if(i==0) cout<<a[i];
  65. else cout<<" "<<a[i];
  66. if(v[i]) cout<<" /";
  67. }
  68. cout<<endl;
  69. }
  70.  
  71.  
  72. return 0;
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement