POJ2456 Aggressive cows(二分)
链接:http://poj.org/problem?id=2456
题意:一个数轴上n个点,每个点一个整数值,有c个奶牛,要放在这些点的某几个上,求怎么放可以使任意两个奶牛间距离的最小值最大,求这个最大值。
思路:仍然是最大化最小值,套路一样,二分最小距离,每次check即可
AC代码:
#include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
const int maxn = 1e5+;
typedef long long ll;
ll sta[maxn];
int N,C;
bool check(ll dis){
int sum = ;
int cur = ;
for(int i = ;i<N;i++){
if(sta[i] - sta[cur]>=dis){
sum++;
cur = i;
}
}
return sum>=C;
}
int main(){
while(cin>>N>>C){
for(int i = ;i<N;i++){
cin>>sta[i];
}
sort(sta,sta+N);
ll MAX = -;
for(int i = ;i<N;i++){
MAX = max(MAX,sta[i]-sta[i-]);
}
ll l = ,r = MAX *;
ll mid;
while(l<r){
mid = (+l+r)>>;
if(check(mid)){//sum>=C;
l = mid ;
}
else{
r = mid - ;
}
}
// cout<<l<<" "<<r<<endl;
cout<<l<<endl;
}
return ;
}
POJ2456 Aggressive cows(二分)的更多相关文章
- POJ2456 Aggressive cows 二分
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- POJ2456 Aggressive cows(二分+贪心)
如果C(d)为满足全部牛之间的距离都不小于d. 先对牛舍的位置排序,然后二分枚举d,寻找满足条件的d. #include<iostream> #include<cstdio> ...
- 二分法的应用:最大化最小值 POJ2456 Aggressive cows
/* 二分法的应用:最大化最小值 POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
- 二分算法的应用——最大化最小值 POJ2456 Aggressive cows
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description Far ...
- POJ2456 Aggressive cows
Aggressive cows 二分,关键是转化为二分! #include <cstdio> #include <algorithm> ; ; int N, C; int a[ ...
- POJ 2456 Aggressive cows(二分答案)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...
- POJ2456 Aggressive cows 2017-05-11 17:54 38人阅读 评论(0) 收藏
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13993 Accepted: 6775 ...
- POJ - 2456 Aggressive cows 二分 最大化最小值
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18099 Accepted: 8619 ...
- POJ 2456 Aggressive cows (二分 基础)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7924 Accepted: 3959 D ...
- poj2456 Aggressive cows(二分查找)
https://vjudge.net/problem/POJ-2456 二分,从最大长度开始,不断折半试,如果牛全放下了,就是可行,修改下界,否则改上届. #include<iostream&g ...
随机推荐
- MySQL 8 服务器组件
MySQL 服务器包含了一个基于组件的架构,用于扩展服务器功能.服务器和其他组件可以使用组件提供的服务.(在使用服务方面,服务器也是一个组件,等同于其他组件).组件之间交互仅通过他们各自提供的服务. ...
- Python 文件&异常 初学者笔记
文件 读取整个文件 with open('pi_30_digits.txt') as file_object :#Python在当前执行文件目录寻找指定文件#filename = 文件的绝对路径或相对 ...
- 【easyui】treegrid逐级加载源码
当初看这源码的目的是: 1.treegrid是怎么实现逐级加载树结构的. 解: 见demo,主要就是点击节点的时候会请求后台. 2.treegrid加载后,第二次展开节点会不会再次请求后台. 解:第二 ...
- 调用手机摄像头并上传图片--jquery ajax
1.图片框样式与进度条样式 .alert_img_content { width: 44%; float: left; margin: 3%; border: 1px solid #ddd; back ...
- python的for循环的神奇之处
python的for循环太神奇了: 你可以编写这样的语句: for i in range(10) : j= i**2 print(j) 你也可以编写这样的语句: with open('/path/to ...
- 2级搭建类203-Oracle 19c SI ASM 静默搭建(OEL7.7)
Oracle 19c 单实例 ASM UDEV 方式在 OEL 7.7 上的安装
- python接口自动化-requests-toolbelt处理multipart/form-data
1.requests-toolbelt官方文档:https://pypi.org/project/requests-toolbelt/ 2.环境安装 pip install requests-tool ...
- ZOJ1310-Robot (BFS)
The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...
- Python中pip的使用
1.pip安装模块 pip install 模块名称 -i 安装源 pip install requests -i https://mirrors.aliyun.com/pypi/simple/
- pick the stone game
我该如何去触摸这类问题嘞! 取石子游戏 1堆石子有n个,两人轮流取. 先取者第1次可以取任意多个,但不能全部取完. 以后每次取的石子数不能超过上次取子数的2倍. 取完者胜.先取者负输出"Se ...