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

题目链接:

nyoj : http://acm.nyist.net/JudgeOnline/problem.php?pid=586

poj : http://poj.org/problem?id=2456

思路:

二分答案,从前到后依次排放m头牛的位置,检查是否可行

代码:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn = 1e5+5;
const int INF = 1e9;
int a[maxn];
int n,m;
bool check(int d) {
int pos,pre;
pre=0;
for(int i=0;i<m-1;++i) {
pos=pre+1;
while(pos<n&&a[pos]-a[pre]<d) {pos++;}
if(pos==n) return false;
pre=pos;
}
return true;
}
int main() {
while(~scanf("%d %d",&n,&m)) {
for(int i=0;i<n;++i) scanf("%d",&a[i]);
sort(a,a+n);
int l=0,r=INF,mid,ans;
while(l<=r) {
mid=(l+r)>>1;
if(check(mid)) ans=mid,l=mid+1;
else r=mid-1;
}
printf("%d\n",ans);
}
return 0;
}

poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分的更多相关文章

  1. poj 2456 Aggressive cows(二分搜索之最大化最小值)

    Description Farmer John has built a <= N <= ,) stalls. The stalls are located along a straight ...

  2. 二分搜索 POJ 2456 Aggressive cows

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

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

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

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

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

  5. POJ 2456 Aggressive cows (二分)

    题目传送门 POJ 2456 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) s ...

  6. POJ 2456 Aggressive cows

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11192   Accepted: 5492 ...

  7. [POJ] 2456 Aggressive cows (二分查找)

    题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...

  8. POJ 2456 Aggressive cows ( 二分搜索)

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

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

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

随机推荐

  1. 双十一临近,怎样让买家流畅地秒杀? ——腾讯WeTest独家开放电商产品压测服务

    WeTest 导读 十一月临近,一年一度的电商大戏"双十一"又将隆重出场,目前各大商家已经开始各类优惠券的发放,各类大促的商品表单也已经提前流出,即将流入各个用户的购物车中. 作为 ...

  2. SEO诊断之关于网站收录(转)

    如何让网站被搜索引擎收录?我的网站有收录无排名怎么办?这些网站收录问题估计是seo最应关心的根本问题之一,网站收录都没有何来排名?我整理了每天咨询最多最具代表性的 8 个关于网站收录的问题及其答案统一 ...

  3. Ignatius and the Princess II

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

  4. SSH框架整合--applicationContext.xml文件配置实例

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  5. jsp静态与动态包含的区别和联系

    1. <%@ include file=” ”%>是指令元素.<jsp:include page=” ”/>是行为元素 2. 最终编译成java文件的数目不同. * 静态包含在 ...

  6. 用Nodejs+Express搭建web,nodejs路由和Ajax传数据并返回状态,nodejs+mysql通过ajax获取数据并写入数据库

    小编自学Nodejs,看了好多文章发现都不全,而且好多都是一模一样的 当然了,这只是基础的demo,经供参考,但是相信也会有收获 今天的内容是用Nodejs+Express搭建基本的web,然后呢no ...

  7. 阿里云centos 搭建SVN

    1. 安装 先进入想安装的目录,执行 yum install subversion 2. 创建SVN目录 mkdir -p /var/svn/svnrepos  ,然后创建版本库 svnadmin c ...

  8. 获取对象属性(key)组成的数组 Object.keys( obj ).md

    Object.keys() 方法会返回一个由给定对象的自身可枚举属性组成的数组,数组中属性名的排列顺序和使用 for...in 循环遍历该对象时返回的顺序一致 (两者的主要区别是 一个 for-in ...

  9. 【1】hadoop搭建常用的Linux命令收集

    大数据学习第一步:Linux入门学习所需要的命令(非基础却常用) chmod u+x /filepath/../filename : u.g.o表示三个不同组,r.w.x表示可读可写可执行,+.-表示 ...

  10. 如何在工程中使用axis2部署webservice

    有一个最简单的方法就是把axis2.war中的内容作为Web Project的基础, 来进行开发. 不过为了更清楚的了解如何在一个已有的Web Project中嵌入axis2, 那就手动来配置.大致分 ...