题目链接:
题目大意:
有n个牛栏,选m个放进牛,相当于一条线段上有 n 个点,选取 m 个点, 使得相邻点之间的最小距离值最大
解题思路:
二分枚举最小距离的最大值
1 #include2 #include 3 #include 4 #include 5 using namespace std; 6 const int INF = 1e9; 7 const int maxn = 100005; 8 int n, m; 9 int a[maxn];10 bool judge(int x)11 {12 int last = 1;13 for(int i = 1; i < m; i++)14 {15 int now = last + 1;16 while(now <= n && a[now] - a[last] < x)now++;17 last = now;18 }19 return last <= n;20 }21 int main()22 {23 cin >> n >> m;24 for(int i = 1; i <= n; i++)cin >> a[i];25 sort(a + 1, a + n + 1);26 int l = 0, r = INF + 10, ans;27 while(l <= r)28 {29 int mid = (l + r) / 2;30 if(judge(mid))31 ans = mid, l = mid + 1;32 else r = mid - 1;33 }34 cout< <