Description

Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000).
His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them
is as large as possible. What is the largest minimum distance?

农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1,000,000,000). 但是,John的C (2 <= C <= N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗。为了不让牛互相伤害。John决定自己给牛分配隔间,使任意两头牛之间的最小距离尽可能的大,那么,这个最大的最小距离是什么呢

Input

* Line 1: Two space-separated integers: N and C * Lines 2..N+1: Line i+1 contains an integer stall location, xi

第一行:空格分隔的两个整数N和C

第二行---第N+1行:i+1行指出了xi的位置

Output

* Line 1: One integer: the largest minimum distance

第一行:一个整数,最大的最小值

Sample Input

5 3

1

2

8

4

9


Sample Output

3



把牛放在1,4,8这样最小距离是3

题意是给定数轴上n个点,要取出其中m个点使得相邻的点的最小距离最大。

看到最小值最大……呵呵……果断二分

每次枚举一个最小距离k,然后贪心验证。

首先第一个点是必取的,然后后面的点一旦超过k就取。因为如果超过k还不取就相当于寻找更“不优”的一组可行解,不取白不取

最后判断一下有没有取光m个点就行了

#include<cstdio>
#include<algorithm>
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,m,ans,l,r;
int a[200001];
inline bool jud(int x)
{
int tot=1,now=1,save=1;
while (tot<=m)
{
while (a[now]-a[save]<x && now<=n) now++;
if (now>n && tot<m) return 0;
tot++;save=now;
if (tot==m) return 1;
}
return 1;
}
int main()
{
n=read();
m=read();
for (int i=1;i<=n;i++) a[i]=read();
sort(a+1,a+n+1);
l=1;r=1000000000;
while (l<=r)
{
int mid=(l+r)>>1;
if (jud(mid)){ans=mid;l=mid+1;}
else r=mid-1;
}
printf("%d",ans);
}

最新文章

  1. Unity Animation System(动画系统)
  2. HA模式下历史服务器配置
  3. HTTP 错误 404.2 - Not Found
  4. R语言学习笔记:因子(Factors)
  5. 扩展Oracle表空间
  6. JavaConcurrentHashMap
  7. cassandra 数据到Java对象的映射绑定
  8. C语言极易出错的地方(更新中)
  9. java初阶
  10. BZOJ.5287.[AHOI HNOI2018]毒瘤(虚树 树形DP)
  11. androidstudio全局搜索快捷键Ctrl+Shift+F失效的解决办法
  12. Python学习笔记-常用内置函数
  13. Java 8新特性----Lambda
  14. .NET高级代码审计(第四课) JavaScriptSerializer反序列化漏洞
  15. leetcode15&mdash;3Sum
  16. (弃) Keystone CLI_可选命令详解
  17. asp.net mvc中配置路由默认值(Area中)
  18. Tensorflow(二)
  19. JAVA Spring 面向切面编程 基本案例(AOP)
  20. C语言程序设计I—第七周教学

热门文章

  1. 合天网安实验室学习笔记----Linux基础
  2. 关于bootstrap--排版(标题、强调、背景、插入符等)
  3. Html5 Canvas 实现滚动的图片
  4. UIApplication的作用
  5. EF中的连接字符串
  6. oracle时间戳转换
  7. MVC学习四
  8. Git远程使用技巧
  9. jdbc oracle 连接字符串
  10. hdu 1234