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. ...
随机推荐
- P1816忠诚
这是一个区间查询最值的问题,用线段树来做. 建树的时候,这里不是求和,应该是e[k].w=min(e[k*2].w,e[k*2+1].w),所以这里要注意以下,其次是查询的时候,因为本题不用让我们修改 ...
- # 匈牙利算法(二分图最大匹配)- hdu 过山车
匈牙利算法(二分图最大匹配)- hdu 过山车 Hdu 2063 二分图:图中的点可以分成两组U,V,所有边都是连接U,V中的顶点.等价定义是:含奇数条边的图. 匹配:一个匹配是一个边的集合,其中任意 ...
- Fiddler--模拟弱网
1.首先要勾选Rules--Performance--Simulate Modem Speeds 2.点击Customer Rules 3.然后找到 修改这个值就好. 备注:修改完保存后,要重新勾选第 ...
- 生成ini文件
setProfileString是无法直接生成ini文件的,如果不存在这个ini文件需要先创建,然后再setProfileString.示例代码//保存连接参数到配置文件if not FileExis ...
- [Nest] 03.nest之提供者 provider
提供者 provider 提供程序是 Nest 的一个基本概念.许多基本的 Nest 类可能被视为提供者 - service,repository, factory, helper 等等. 他们都可以 ...
- Nginx负载均衡调度算法
Nginx支持的负载均衡调度算法方式如下: 1. weight轮询(默认) 接收到的请求按照顺序逐一分配到不同的后端服务器,即使在使用过程中,某一台后端服务器宕机,nginx会自动将该服务器剔除出队列 ...
- Scala新版本学习(1):
1.进官网:https://www.scala-lang.org/ 上面就是进入Scala社区后的一个画面,官方对Scala的简单介绍是:Scala将面向对象和函数式编程集合在一个简洁的高级语言中,S ...
- 解决postgresql数据库localhost可以连接,ip连接不了的问题
解决:windows环境下,postgresql数据库,localhost可以连接,ip地址连接不了. 解决办法: 1.打开postgresql安装目录下的配置文件 pg_hba.conf ...
- 2019-2020-1 20199319《Linux内核原理与分析》第五周作业
系统调用的三层机制(上) 基础知识 1.通过库函数的方式进行系统调用,库函数用来把系统调用给封装起来. 2.CPU有四种不同的执行级别:0.1.2.3,数字越小,特权越高.Linux操作系统中采用了0 ...
- c++ 实现https网页上的图片爬取
一.主要的原理 我们通过发送一个http请求,获得目标网页的html源代码,然后通过正则表达式获取到图片的URL,把该网页的所有的图片都保存到一个文件夹,这就是整个软件的流程. 二.具体的实践 现在很 ...