您的位置:首页 > Web前端

BZOJ1734: [Usaco2005 feb]Aggressive cows 愤怒的牛

2017-08-14 18:50 344 查看

n<=100000个点在坐标系上,选m个点使点间最小距离最大。

二分模板??

1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 #include<algorithm>
5 #include<math.h>
6 //#include<iostream>
7 using namespace std;
8
9 int n,m;
10 #define maxn 100011
11 int a[maxn];
12 bool judge(int x)
13 {
14     int last=1,tot=1;
15     for (int i=2;i<=n;i++)
16         if (a[i]-a[last]>=x) tot++,last=i;
17     return tot>=m;
18 }
19 int main()
20 {
21     scanf("%d%d",&n,&m);
22     for (int i=1;i<=n;i++) scanf("%d",&a[i]);
23     sort(a+1,a+1+n);
24     int L=0,R=1e9+1;
25     while (L<R)
26     {
27         int mid=(L+R+1)>>1;
28         if (judge(mid)) L=mid;
29         else R=mid-1;
30     }
31     printf("%d\n",L);
32     return 0;
33 }
View Code

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: