Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- bool ispossible(ll int arr[],ll int n,ll int dist,ll int c)
- {
- ll int cowscompleted=1,lastpos=0;
- for(ll int i=1;i<n;i++)
- {
- if((arr[i]-arr[lastpos])>=dist)
- {
- lastpos=i;
- cowscompleted++;
- if(cowscompleted==c)
- return true;
- }
- }
- return false;
- }
- void binarysearch(ll int arr[],ll int n,ll int c)
- {
- ll int start=0,end=n-1,mid,dist;
- while(end>=start)
- {
- mid=(start+end)/2;
- dist=arr[mid]-arr[0];
- if(ispossible(arr,n,dist,c))
- start=mid+1;
- else
- end=mid-1;
- }
- cout<<arr[start-1]-arr[0]<<endl;
- }
- int main() {
- ll int t;
- cin>>t;
- while(t--)
- {
- ll int n,c;
- cin>>n>>c;
- ll int x[n];
- for(ll int i=0;i<n;i++)
- cin>>x[i];
- sort(x,x+n);
- binarysearch(x,n,c);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment