首页 > 其他 > 详细

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

时间:2018-04-29 17:33:49      阅读:284      评论:0      收藏:0      [点我收藏+]

题目链接:

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 }

 

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

原文:https://www.cnblogs.com/fzl194/p/8971358.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!