您的位置:首页 > 其它

【基础练习】【二分】codevs2072 分配房间题解

2015-10-15 16:48 274 查看
题目描述 Description

yh拥有一条街道,街道上共有n间房子,每间房子的坐标为xi(yh的房子比较神奇,可能重叠)。
同时,yh有m个女朋友(这是事实),yh打算给每位女朋友分配一间房子。两个女朋友间的距离相隔越近,她们之间产生冲突的可能就越高。yh想尽可能的减小女朋友间的冲突,于是他打算让他的女朋友间的最小距离最大,你能帮yh找出这个最大值吗?

输入描述 Input Description

第一行两个整数,n,m,表示yh有n间房子,有m个女朋友
第二行为n个整数,xi,表示各间房子的坐标。

输出描述 Output Description

输出1行,表示yh女朋友间的最小距离的最大值

样例输入 Sample Input

5 3
1 2 8 4 9

样例输出 Sample Output

3

数据范围及提示 Data Size & Hint

对于30%的数据,n<=100,m<=n,0<=xi<=10000;
对于100%的数据,n<=100000 ,m<=n,0 <= xi <= 1000000000

如今这种层次的二分完全是水题一道···

//codevs2072 ·ÖÅä·¿¼ä ¶þ·Ö
//copyright by ametake
#include
#include
#include
using namespace std;

const int maxn=100000+10;
int a[maxn];
int n,m;

bool can(int x)
{
int cnt=0,last=1;
int i=1;
while (i<=n)
{
while (a[i]-a[last]=m;
}

int main()
{
freopen("1.txt","r",stdin);
freopen("2.txt","w",stdout);
scanf("%d%d",&n,&m);
int l=1,r=0;
for (int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
sort(a+1,a+n+1);
r=a
;
while (l>1;
if (can(mid))
{
l=mid;
}
else r=mid-1;
}
printf("%d\n",l);
return 0;
}


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