POJ 2456 编程技巧之------二分查找思想的巧妙应用
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 18599 | Accepted: 8841 | 
Description
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?
Input
* Lines 2..N+1: Line i+1 contains an integer stall location, xi
Output
Sample Input
5 3
1
2
8
4
9
Sample Output
3
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define INF 999999999
const int MAX_N=100005;
int N,M;
int x[MAX_N];
//判断是否满足条件
bool C(int d)
{
    int last=0;
    for(int i=1;i<M;i++)
    {
        int crt=last+1;
        while(crt<N&&x[crt]-x[last]<d)
        {
            crt++;
        }
        if(crt==N)return false;
        last = crt;
    }
    return true;
}
void solve()
{
    sort(x,x+N);
    int lb=0,ub=INF;
    int mid;
    while(ub-lb>1)
    {
        mid=(lb+ub)/2;
        if(C(mid))lb=mid;
        else ub=mid;
    }
    printf("%d\n",lb);
}
int main()
{
    //freopen("C://Users/Administrator/Desktop/acm.txt","r",stdin);
    scanf("%d%d",&N,&M);
    for(int i=0;i<N;i++)
    {
        scanf("%d",&x[i]);
    }
    solve();
    return 0;
}
POJ 2456 编程技巧之------二分查找思想的巧妙应用的更多相关文章
- [ACM] poj 2456 Aggressive cows (二分查找)
		
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5436 Accepted: 2720 D ...
 - POJ 1064 Cable master(二分查找+精度)(神坑题)
		
POJ 1064 Cable master 一开始把 int C(double x) 里面写成了 int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条 ...
 - POJ 2456 Aggressive cows (二分)
		
题目传送门 POJ 2456 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) s ...
 - poj 2456 Aggressive cows  贪心+二分
		
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25944 Accepted: 11982 ...
 - noi题库(noi.openjudge.cn) 1.11编程基础之二分查找T01、02、04
		
T01 查找最接近的元素 描述 在一个非降序列中,查找与给定值最接近的元素. 输入 第一行包含一个整数n,为非降序列长度.1 <= n <= 100000.第二行包含n个整数,为非降序列各 ...
 - POJ 1019:Number Sequence 二分查找
		
Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36013 Accepted: 10409 ...
 - [ACM] poj 1064 Cable master (二分查找)
		
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21071 Accepted: 4542 Des ...
 - POJ 3258 River Hopscotch(二分查找答案)
		
一个不错的二分,注释在代码里 #include <stdio.h> #include <cstring> #include <algorithm> #include ...
 - POJ 1064     Cable master     (二分查找)
		
题目链接 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. ...
 
随机推荐
- IOS订阅优惠-PHP生成ECDSA算法签名
			
<?php use Ramsey\Uuid\Uuid; class ItunesSignatureGenerator { private $appBundleID = 'www.u17.com' ...
 - IntelliJ IDEA 复杂的重构技巧
			
IntelliJ IDEA 复杂的重构技巧(二) 转载 上次我说了一些 "复杂的重构技巧" ,讲的是一些使用 IntelliJ 的简单功能实现复杂的重构需求的技巧. 看到大家的反响 ...
 - C数据结构排序算法——直接插入排序法用法总结(转http://blog.csdn.net/lg1259156776/)
			
声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 排序相关的的基本概念 排序:将一组杂乱无章的数据按一定的规律顺次排列起来. 数据表( data list): ...
 - spring boot本地开发与docker容器化部署的差异
			
spring boot本地开发与docker容器化部署的差异: 1. 文件路径及文件名区别大小写: 本地开发环境为windows操作系统,是忽略大小写的,但容器中区分大小写 2. docker中的容器 ...
 - docker之常用命令
			
1) docker run -p : --name mysql -v d:/docker/mysql/conf:/etc/mysql/conf.d -v d:/docker/mysql/logs:/l ...
 - 查询SAP数据库表的大小
			
事物代码DB02 Perfomrmance->Additional Functions->SQL Command Editor->写数据表->执行 select bytes/1 ...
 - 创建全文索引----SQLserver
			
1.启动 Microsoft Search 服务 开始菜单-->SQL程序组-->服务管理器-->下拉筐-->Microsoft Search 服务-->启动它. 2. ...
 - LLVM 安装教程(包安装)
			
LLVM 安装教程 环境:ubuntu16.04 llvm-4.0 clang-4.0 步骤: 1.依赖库安装 $ sudo apt-get install build-essential curl ...
 - xml_SAX解析
			
(一)SAX解析 1.1 SAX解析 SAX:基于事件处理的机制 sax解析xml文件时,遇到根开始标签,根结束标签,开始解析文件,文件解析结束,字符内容,空白字符等都会触发各自的方法. 优点: 适合 ...
 - 用ant打包
			
Eclipse 内置了 Ant . Ant 是一种类似于批处理程序的软件包,它主要繁琐的工作是编写和调试自动处理脚本(一个 XML 文件),但只要有了这个脚本,我们就可以一键完成所有的设定工作. 本节 ...