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. ...
随机推荐
- python控制流 -- if、for、while、range()、sys.exit()
1.布尔值 “布尔”数据类型只有两种:True和False #首字母以T或F开头,后面小写,且不能作为变量赋值 2.比较操作符 == 等于 != 不等于 < 小于 > 大于 &l ...
- Cypher查询在Neo4j中加载具有点数据类型属性的CSV文件
我有一个CSV文件,标有3列,ID,纬度,经度.我想将CSV文件加载到Neo4j中并创建具有上述属性的节点.Location:属性应该是具有纬度和经度子属性的点数据类型. CSV是: ID,latit ...
- [Next] 三.next自定义服务器和路由
next 服务端渲染 实际上,next 一直都是执行的服务端渲染.npm start执行的是 next 自带的服务器来运行你的应用.next 是支持自定义服务器的,同时能够支持现有的路由和模式,你可以 ...
- java之设计模式汇总
1.单例模式 就是一个类只产生一个对象 对应数据库连接 定时执行者服务(ScheduledExecutorService) 在整个项目中应该只有一个对象 2.工厂模式 定义一个用于创建对象的接口 让子 ...
- Jquery 控制table中的checkbox 相关选中事件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- scrapy-redis 实现分布式爬虫
分布式爬虫 一 介绍 原来scrapy的Scheduler维护的是本机的任务队列(存放Request对象及其回调函数等信息)+本机的去重队列(存放访问过的url地址) 所以实现分布式爬取的关键就是,找 ...
- js 继承的简单理解
什么是继承 js中的继承就是获取存在对象已有属性和方法的一种方式. 继承一 属性拷贝 就是将对象的成员复制一份给需要继承的对象. // 创建父对象 var superObj = { name:'liy ...
- Linux和Windows双系统下Windows系统插入耳机没有声音
我的笔记本装了Windows7和Debian双系统后,在Windows7下,插入耳机竟然没有声音. 按常规思路分析:首先考虑是耳机问题还是笔记本电脑问题.确定耳机没问题后问题就在笔记本身上了.而问题在 ...
- MySQL中导出用户权限设置的脚本
在对MySQL数据库进行迁移的时候,有时候也需要迁移源数据库内的用户与权限.对于这个迁移我们可以从mysql.user表来获取用户的相关权限来生成相应的SQL语句,然后在目标服务器上来执行生成的SQL ...
- 学习Linux让我进入了知名企业 原
说起我学习Linux的原因是多方面的,大学时我学的是物理学师范专业,有部分计算机课程,但我觉得这些课程没什么实际作用,我自己对计算机比较感兴趣,我利用业余时间学习了很多计算机技术.在大学期间我参加了很 ...