如果C(d)为满足全部牛之间的距离都不小于d。

先对牛舍的位置排序,然后二分枚举d,寻找满足条件的d。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#define ll __int64
#define INF 0x3fffffff
using namespace std; int n,m;
int x[100005]; bool C(int d)
{
int num=1;
int a=x[0];
int i=1;
while(num<m)
{
if(a+d<=x[i])
{
num++;
a=x[i];
i++;
}
else i++;
if(i==n) break;
}
if(num==m) return true;
else return false;
} void solve()
{
int l=0,r=x[n-1]+1;
while(r-l>1)
{
int mid=(r+l)/2;
if(C(mid)) l=mid;
else r=mid;
}
cout<<l<<endl;
} int main()
{
//freopen("d:\\Test.txt","r",stdin);
cin>>n>>m;
for(int i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
sort(x,x+n);
solve();
return 0;
}

POJ2456 Aggressive cows(二分+贪心)的更多相关文章

  1. POJ2456 Aggressive cows 二分

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

  2. POJ 2456 Aggressive cows ( 二分 && 贪心 )

    题意 : 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1e9) ...

  3. 二分法的应用:最大化最小值 POJ2456 Aggressive cows

    /* 二分法的应用:最大化最小值 POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...

  4. 二分算法的应用——最大化最小值 POJ2456 Aggressive cows

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description Far ...

  5. POJ 2456 Aggressive cows(贪心 + 二分)

    原题链接:Aggressive cows 题目大意:农夫 建造了一座很长的畜栏,它包括  个隔间,这些小隔间依次编号为. 但是, 的  头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争 ...

  6. POJ 2456 Aggressive cows(二分答案)

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...

  7. POJ2456 Aggressive cows

    Aggressive cows 二分,关键是转化为二分! #include <cstdio> #include <algorithm> ; ; int N, C; int a[ ...

  8. POJ2456 Aggressive cows 2017-05-11 17:54 38人阅读 评论(0) 收藏

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13993   Accepted: 6775 ...

  9. POJ - 2456 Aggressive cows 二分 最大化最小值

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18099   Accepted: 8619 ...

随机推荐

  1. 非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)

    非变动性算法代码分析与示例: 一.for_each  C++ Code  1 2 3 4 5 6 7 8 9 10 11 12 13 14   // TEMPLATE FUNCTION for_eac ...

  2. Node.js验证码模块captchapng

    captchapng是一个基于pnglib模块开发,数字型验证码模块.内置字体.全JavaScript无其它依赖.不像有的验证码需要依赖canvas或者是需要编译,而且captchapng使用起来简单 ...

  3. Memory leak patterns in JavaScript

    Handling circular references in JavaScript applications Plugging memory leaks in JavaScript is easy ...

  4. Atitit.js javascript异常处理机制与java异常的转换.js exception process Voae

    Atitit.js javascript异常处理机制与java异常的转换.js exception processVoae 1. 1. javascript异常处理机制 1 2. 2. Web前后台异 ...

  5. Leetcode[81]-Search for a Range

    Link: https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ Given a sorted array of integ ...

  6. Coreseek:第一步配置文件

    Windows操作系统下:mysql数据源配置:(相应coreseek-3.2.13-win32/etc/csft_mysql.conf) #源定义 source mysql { type = mys ...

  7. ENGINE_API CXNoTouch

    /************************************************************************/ //屏蔽消息面板 //优先级默认为 TP_BOTT ...

  8. 三、请求库之requests模块

    一 介绍   #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:requests库发送请求将网 ...

  9. ng-class中的if else判断

    ng-class中的if else判断 来自于stackoverflow的一个问题,自己刚好用到,搬过来做个标记.原问题链接 在使用ng-class时,有些时候会碰到根据是否满足条件来设置元素的样式, ...

  10. python跳一跳辅助学习

    微信跳一跳辅助工具 准备工具 adb驱动 安卓手机 打开手机的调试模式 usb接好手机和电脑 PyCharm:全宇宙唯一一款专门用于Python开发IDE工具 实现原理: 获取手机的实时的截图 点击起 ...