Ankit_132

Dividing into Groups

May 31st, 2023
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define ll     long long
  7. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  8. #define pb     push_back
  9.  
  10.  
  11. int main()
  12. {
  13.  
  14.     _test
  15.     {
  16.         ll int n, k;
  17.         cin>>n>>k;
  18.  
  19.         vector<ll int> a(n);
  20.         for(auto &e: a)
  21.             cin>>e;
  22.  
  23.         sort(a.rbegin(), a.rend());
  24.  
  25.         ll int low=0, high=1e18;
  26.         ll int ans = 0;
  27.  
  28.         auto possible = [&](ll int x)
  29.         {
  30.             ll int tot=0, got=0;
  31.             for(int i=0; i<n; i++)
  32.             {
  33.                 if(got == 0)
  34.                 {
  35.                     if(a[i] >= x)
  36.                     {
  37.                         tot++;
  38.                         continue;
  39.                     }
  40.                     else
  41.                         got += a[i];
  42.                 }
  43.                 else
  44.                 {
  45.                     got += a[i];
  46.                     if(got >= x)
  47.                     {
  48.                         got -= x;
  49.                         tot++;
  50.                     }
  51.                 }
  52.             }
  53.  
  54.             return (tot>=k);
  55.         };
  56.  
  57.         while(low <= high)
  58.         {
  59.             ll int mid = (low + high)/2;
  60.  
  61.             if(possible(mid))
  62.                 ans=mid, low=mid+1;
  63.             else
  64.                 high=mid-1;
  65.         }
  66.  
  67.         cout<<ans<<"\n";
  68.     }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment