POJ-2456 Aggressive cows---最大化最小值(也就是求最大值)
阿新 • • 發佈:2018-04-29
思路 cst target main include IT urn cstring strong
題目鏈接:
https://vjudge.net/problem/POJ-2456
題目大意:
有n個牛欄,選m個放進牛,相當於一條線段上有 n 個點,選取 m 個點, 使得相鄰點之間的最小距離值最大
解題思路:
二分枚舉最小距離的最大值
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 elser = mid - 1; 33 } 34 cout<<ans<<endl; 35 return 0; 36 }
POJ-2456 Aggressive cows---最大化最小值(也就是求最大值)