fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. bool check(int cows,long long int positions[],int n,long long int distance){
  4. int count = 1;
  5. long long last_position = positions[0];
  6.  
  7. for(int i=1;i<n;i++){
  8. if(positions[i] - last_position >= distance){
  9. last_position = positions[i];
  10. count++;
  11. }
  12.  
  13. if(count == cows){
  14. return true;
  15. }
  16. }
  17. return false;
  18. }
  19. int main() {
  20.  
  21. int t;
  22. cin>>t;
  23. while(t--){
  24. int n,c;
  25. cin>>n>>c;
  26. long long int positions[n];
  27. for(int i=0;i<n;i++){
  28. cin>>positions[i];
  29. }
  30. sort(positions,positions+n);
  31. long long int start = 0;
  32. long long int end = positions[n-1]-positions[0];
  33. long long int ans = -1;
  34. while(start<=end){
  35. long long int mid = start + (end-start)/2;
  36. if(check(c,positions,n,mid)){
  37. ans = mid;
  38. start = mid+1;
  39. }else{
  40. end = mid - 1;
  41. }
  42. }
  43. cout<<ans<<endl;
  44. }
  45. return 0;
  46.  
  47. }
Success #stdin #stdout 0s 4220KB
stdin
Standard input is empty
stdout
Standard output is empty