POJ-2456 Aggressive cows---最大化最小值(也就是求最大值)

题目链接:

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         else r = mid - 1;
33     }
34     cout<<ans<<endl;
35     return 0;
36 }

 

posted @ 2018-04-29 16:55  _努力努力再努力x  阅读(251)  评论(0编辑  收藏  举报