Advertisement
TAHMID37

agreesive cow

Jun 2nd, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. /*  TAHMID RAHMAN
  2.     DAMIAN FOREVER
  3.      MATH LOVER
  4.     NEVER GIVE UP
  5. */
  6. #include<bits/stdc++.h>
  7. using namespace std;
  8. #define pi acos(-1.0)
  9. #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  10. #define ll long long
  11. #define pb push_back
  12. #define fi first
  13. #define se second
  14. #define in insert
  15. #define mp make_pair
  16. #define GCD(a,b) __gcd(a,b);
  17. #define endl "\n"
  18. #define FRU freopen("out.txt","w",stdout)
  19. #define FRO freopen("in.txt","r",stdin)
  20. #define INFLL 9223372036854775807
  21. //Don't hesitate to ask me if you don't understand my code.......Happy coding,Tahmid...;
  22. ll a[100001];
  23. bool istrue(ll mid,ll n,ll c)
  24. {
  25.     ll cnt=0,i,pref=-INT_MAX;
  26.     for(i=1;i<=n;i++)
  27.     {
  28.        if(a[i]-pref>=mid)
  29.        {
  30.            cnt++;
  31.            pref=a[i];
  32.        }
  33.     }
  34.     if(cnt>=c)
  35.         return true;
  36.     else
  37.         return false;
  38. }
  39. ll bs(ll n,ll c)
  40. {
  41.     ll l=0,h=a[n]-a[1],mid,ans=0;
  42.     while(l<h)
  43.     {
  44.         mid=(l+h)/2 ;
  45.         if(istrue(mid,n,c))
  46.            ans=max(ans,mid),l=mid+1;
  47.         else
  48.             h=mid-1;
  49.     }
  50.     return ans;
  51. }
  52.  
  53. int main()
  54. {
  55.     fastio;
  56.     ll t;
  57.     cin>>t;
  58.     while(t--)
  59.     {
  60.         ll n,c,i;
  61.         cin>>n>>c;
  62.         for(i=1;i<=n;i++)
  63.         {
  64.             cin>>a[i];
  65.         }
  66.         sort(a+1,a+n+1);
  67.         cout<<bs(n,c)<<endl;
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement