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

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 建造了一座很长的畜栏,它包括NN (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
题解:
很明显的二分答案。。
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=;
int n,m,i,a[N];
int erfen(int l,int r)
{
if(l>r) return r;
int mid=(l+r)>>,x=a[],k=;
for(i=;i<=n;i++)
if(a[i]-x>=mid)
{
x=a[i];
k++;
}
if(k>=m) return erfen(mid+,r);else return erfen(l,mid-);
}
int main()
{
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
scanf("%d",&a[i]);
sort(a+,a+n+);
cout<<erfen(,a[n]-a[]);
return ;
}
 

bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛的更多相关文章

  1. BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )

    最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...

  2. bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛【二分+贪心】

    二分答案,贪心判定 #include<iostream> #include<cstdio> #include<algorithm> using namespace ...

  3. 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛

    1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 217  Solved: ...

  4. bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案

    [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 407  Solved: 325[S ...

  5. bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛

    Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...

  6. B1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案

    水题,20分钟AC,最大值最小,一看就是二分答案... 代码: Description Farmer John has built a <= N <= ,) stalls. The sta ...

  7. BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛( floyd + 二分答案 + 最大流 )

    一道水题WA了这么多次真是.... 统考终于完 ( 挂 ) 了...可以好好写题了... 先floyd跑出各个点的最短路 , 然后二分答案 m , 再建图. 每个 farm 拆成一个 cow 点和一个 ...

  8. [BZOJ 1733] [Usaco2005 feb] Secret Milking Machine 【二分 + 最大流】

    题目链接:BZOJ - 1733 题目分析 直接二分这个最大边的边权,然后用最大流判断是否可以有 T 的流量. 代码 #include <iostream> #include <cs ...

  9. bzoj:1675 [Usaco2005 Feb]Rigging the Bovine Election 竞选划区

    Description It's election time. The farm is partitioned into a 5x5 grid of cow locations, each of wh ...

随机推荐

  1. 如何使用webpack打包你的项目

    webpack是前端开发中比较常用的打包工具之一,另外还有gulp,grunt.之前没有涉及过打包这块,这里介绍一下使用webpack打包的流程. Grunt和Gulp的工作方式是:在一个配置文件中, ...

  2. oracle 的number数据类型

    NUMBER类型细讲:Oracle number datatype 语法:NUMBER[(precision [, scale])]简称:precision --> p      scale   ...

  3. Join vs merge vs lookup

    The obvious benefit of merge over join is the ability to add reject links. I can't upload pictures. ...

  4. jq 浏览器窗口大小发生变化时

    当调整浏览器窗口的大小时,发生 resize 事件: $(selector).resize(); 实例 对浏览器窗口调整大小进行计数: $(window).resize(function() { $( ...

  5. Redis 分片实现 Redis Shard [www]

    Redis 分片实现                                             Redis Shard https://www.oschina.net/p/redis-s ...

  6. cacti (可以利用yum安装cacti的配置)

    [root@localhost ~]# yum install -y epel-release[root@localhost ~]# [root@localhost ~]# yum install - ...

  7. Asp.net网页中DataGridView数据导出到Excel

    经过上网找资料,终于找到一种可以直接将GridView中数据导出到Excel文件的方法,归纳方法如下: 1. 注:其中的字符集格式若改为“GB2312”,导出的部分数据可能为乱码: 导出之前需要关闭分 ...

  8. Find Peak Element——二分查找的变形

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  9. [前端随笔][JavaScript][自制数据可视化] “中国地图”

    说在前面 想自己实现一个可视化的中国地图(可以实现如用户来源省份数据统计功能),网上搜了一下,翻了几页几乎都是第三方库(如echarts.js)实现的,简直不能忍. 不是第三方库不好,只是要花时间去适 ...

  10. Eclipse的工程名有红色的感叹号,工程里面没有显示编译错误

    在导入其他人或配套光盘中的工程时,经常会出现这种错误. 问题的原因: 通常是JRE的版本不同造成的. 解决的办法: 是选择工程名,然后通过在右键菜单中选择build path->configue ...