Topic link:
https://vjudge.net/problem/POJ-2456
Topic meaning:
There are n cattle stalls, select m to put in cattle, which is equivalent to n points on a line segment, select m points, so that the minimum distance between adjacent points is the largest
Problem solving ideas:
The maximum value of the minimum distance of the bisection enumeration
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 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<<ans<<endl; 35 return 0; 36 }