fork download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Ideone
  5. { static boolean Cows(int arr[],int dist,int cows){
  6. int Cnt=1,last=arr[0];
  7. for(int i=1;i<arr.length;i++){
  8. if(arr[i]-last>=dist){
  9. Cnt++;
  10. last=arr[i];
  11. }
  12. if(Cnt>=cows) return true;
  13. }
  14. return false;
  15.  
  16. }
  17. static int BS(int arr[],int C,int N){
  18. int low=1,high=arr[N-1]-arr[0];
  19. while(low<=high){
  20. int mid=(low+high)/2;
  21. if(Cows(arr,mid,C)) low=mid+1;
  22. else high=mid-1;
  23. }
  24. return high;
  25. }
  26. public static void main (String[] args) throws java.lang.Exception
  27. {
  28. Scanner sc=new Scanner(System.in);
  29. int T=sc.nextInt();
  30. while(T>0){
  31. int N=sc.nextInt();
  32. int C=sc.nextInt();
  33. int arr[]=new int[N];
  34. for(int i=0;i<N;i++){
  35. arr[i]=sc.nextInt();
  36. }
  37. Arrays.sort(arr);
  38. System.out.println(BS(arr,C,N));
  39. T--;
  40. }
  41. }
  42. }
Success #stdin #stdout 0.11s 54612KB
stdin
1
5 3
1
2
8
4
9
stdout
3