Math, asked by Enzo4450, 1 year ago

Farmer john has built a new long barn, with n (2 <= n <= 100,000) stalls. The stalls are located along a straight line at positions x1,,xn (0 <= xi <= 1,000,000,000). His c (2 <= c <= n) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, fj wants to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?

Answers

Answered by airasahir
3

Answer:

Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000).

His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?

Input

t – the number of test cases, then t test cases follows.  

* Line 1: Two space-separated integers: N and C

* Lines 2..N+1: Line i+1 contains an integer stall location, xi

Output

For each test case output one integer: the largest minimum distance.

Example

Input:

1

5 3

1

2

8

4

9

Output:

3

#include<iostream>

using namespace std;

#include<algorithm>

int  main()

{

 long long int  t;

   cin>>t;

   while(t--)

    {

     long long int  n,c;

       cin>>n>>c;

       long long int  arr[n+5];

       for(long long int  i=0;i<n;i++)

        {

         cin>>arr[i];

        }

        sort(arr,arr+n);

     long long int  hi=arr[n-1]-arr[0];

     long long int  lo=0;

      /*for(long long int  i=0;i<n-1;i++)

       {

        if(arr[i+1]-arr[i]<lo) lo=arr[i+1]-arr[i];

       }

       */

       long long int  mid;

   //    cout<<"ini lo "<<lo<<" hi "<<hi<<endl;

       int ans=-1;

       while(lo<hi)

        {

         long long int  taken=1;

         long long int  last_pos=0;

              mid=(lo+hi)/2;

                 //cout<<"mid "<<mid<<endl;

             for(long long int  i=1;i<n;i++)//  n-1

              {

              // cout<<"arr[last] "<<arr[last_pos]<<" arr[i ] "<<arr[i]<<endl;

               if(arr[i]-arr[last_pos]>=mid)

                {

                 taken++;

                 if(taken==c) break;

                 last_pos=i;

                }

              }

            //  cout<<"taken "<<taken<<endl;

             

              if(taken<c)

               {

                hi=mid;

               }

               else

               {

                 

                lo=mid+1;

               }

               if(taken==c && mid>ans)

                {

                 ans=mid;

                }

        }

        cout<<ans<<endl;

       

    }

    return 0;

}

Step-by-step explanation:

Similar questions