题目大意:C头牛最大化他们的最短距离
常规题,二分法即可
1 #include <iostream> 2 #include <algorithm> 3 #include <functional> 4 5 using namespace std; 6 7 static int pos[100000]; 8 9 bool judge(const int, const int,const int); 10 void solve(const int, const int); 11 12 int main(void) 13 { 14 int n, c; 15 while (~scanf("%d%d", &n, &c)) 16 { 17 for (int i = 0; i < n; i++) 18 scanf("%d", &pos[i]); 19 sort(pos, pos + n); 20 solve(n, c); 21 } 22 return 0; 23 } 24 25 void solve(const int n, const int c) 26 { 27 int lb = 0, rb = 1000000001, mid; 28 29 while (rb - lb > 1) 30 { 31 mid = (rb + lb) / 2; 32 if (judge(mid, c, n)) lb = mid; 33 else rb = mid; 34 } 35 printf("%d\n", lb); 36 } 37 38 bool judge(const int dist, const int cows_sum, const int n) 39 { 40 int last_pos = 0, cnt; 41 42 for (int i = 1; i < cows_sum; i++) 43 { 44 cnt = last_pos + 1; 45 while (cnt < n && pos[cnt] - pos[last_pos] < dist) 46 cnt++; 47 if (cnt == n) 48 return false; 49 last_pos = cnt; 50 } 51 return true; 52 }