fork download
  1. #include <bits/stdc++.h>
  2.  
  3.  
  4. #define fl(i,n) for(int i = 0; i < n; i++)
  5.  
  6. #define ll long long
  7. #define nl endl
  8. #define pb push_back
  9. #define mp make_pair
  10. #define fi first
  11. #define se second
  12. #define PII pair<int,int>
  13.  
  14. #define EPs 1e-9
  15. #define INF 1e16
  16.  
  17. #define setBit(i,n) (i|1<<n)
  18. #define clearBit(i,n) (i&(~(1<<n)))
  19. #define toggleBit(i,n) (i^1<<n)
  20. #define isOn(i,n) ((i&1<<n) != 0)
  21.  
  22.  
  23. using namespace std;
  24.  
  25.  
  26.  
  27. ll n, c;
  28. ll a[1000000];
  29.  
  30.  
  31. bool check(ll x){
  32. ll cows = 0, last = a[0];
  33. cows++;
  34. for(int i = 1; i < n; i++){
  35. if(a[i]-last >= x){
  36. cows++;
  37. if(cows == c) return true;
  38. last = a[i];
  39. }
  40. }
  41. return false;
  42. }
  43.  
  44.  
  45. ll binsearch(){
  46. ll low = 0, high = INF, mid;
  47. while(low+1 < high){
  48. mid = (low+high)>>1;
  49. if(check(mid)){
  50. low = mid;
  51. }
  52. else
  53. high = mid;
  54. }
  55. return low;
  56. }
  57.  
  58.  
  59.  
  60. int main()
  61. {
  62. int t;
  63. cin >> t;
  64. while(t--){
  65. cin >> n >> c;
  66. for(int i = 0; i < n; i++) cin >> a[i];
  67. sort(a,a+n);
  68. cout << binsearch() << nl;
  69. }
  70.  
  71.  
  72. return 0;
  73. }
  74.  
Success #stdin #stdout 0s 23056KB
stdin
1
5 3
1
2
8
4
9
stdout
3