题目地址:http://poj.org/problem?id=2456

最大化最小值问题。二分牛之间的间距,然后验证。

 #include<cstdio>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<stdbool.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
#define clr(x,y) memset(x,y,sizeof(x))
#define sqr(x) ((x)*(x))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define LL long long
#define INF 0x3f3f3f3f
#define A first
#define B second
const int N=+;
int n,m,
a[N]; bool check(int d)
{
int last=;
for(int i=;i<m;i++) {
int crt=last+;
while(crt<n && a[crt]-a[last]<d) {
crt++;
}
if(crt==n) return false;
last=crt;
}
return true;
} void solve()
{
sort(a,a+n);
int lb=,ub=INF; while(ub-lb>) {
int mid=(lb+ub)/;
if(check(mid)) {
lb=mid;
} else {
ub=mid;
}
}
printf("%d\n",lb);
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++) {
scanf("%d",&a[i]);
}
solve(); return ;
}

[POJ] 2456 Aggressive cows (二分查找)的更多相关文章

  1. POJ 2456 Aggressive cows (二分 基础)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7924   Accepted: 3959 D ...

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

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

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

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

  4. poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2456 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...

  5. [poj 2456] Aggressive cows 二分

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

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

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

  7. poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分

    poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...

  8. 二分搜索 POJ 2456 Aggressive cows

    题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...

  9. POJ 2456 Agressive cows(二分)

    POJ 2456 Agressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2≤N≤100,000)个隔间,这 些小隔间的位置为x0,...,xN-1 (0≤xi≤1,000,0 ...

随机推荐

  1. WCF-IIS-PDA

    PDA调用WCF 一 IIS托管WCF 项目从开始是用IIS托管的WCF,但一直出错,到最后也没有搞定,希望哪位大神知道的话可以指点. 错误如下: There was no endpoint list ...

  2. BZOJ1671: [Usaco2005 Dec]Knights of Ni

    1671: [Usaco2005 Dec]Knights of Ni Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 175  Solved: 107[Su ...

  3. 2015第24周一Spring事务

    1. Spring事务管理简介 (1)Spring为多种不同类型的事务管理机制提供统一编程模型,这些事务管理模型包括JTA.JDBC.Hibernate.JPA和JDO. (2)Spring支持声明式 ...

  4. Paint House II 解答

    Question There are a row of n houses, each house can be painted with one of the k colors. The cost o ...

  5. strcat函数的坑点

    我们先看下面这样一段代码: #include <iostream> #include <stdlib.h> using namespace std; int main() { ...

  6. 潘石屹的SOHO小报猝死

    东莞时报多媒体数字报刊平台 潘石屹的SOHO小报猝死

  7. 常用文件的文件头(附JAVA测试类)

    1. MIDI (mid),文件头:4D546864 2. JPEG (jpg),文件头:FFD8FF 3. PNG (png),文件头:89504E47 4. GIF (gif),文件头:47494 ...

  8. mini2440裸机试炼之——DMA直接存取 实现Uart(串口)通信

    这个仅仅能作为自己初步了解MDA的开门篇 实现功能: 将字符串数据通过DMA0通道传递给UTXH0,然后在终端 显示.传输数据完后.DMA0产生中断,beep声, LED亮. DMA基本知识 计算机系 ...

  9. Linux 统计代码行数命令

    wc -l `find . -name '*.js'` wc -l `find . -regex ".*\.js"`

  10. gitosis随记

    0.创建git用户 useradd -m git passwd git 1.安装脚本工具(gitosis依赖python) apt-get install python-setuptools 2.gi ...