POJ2456 Aggressive cows 二分
Description
Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,…,xN (0 <= xi <= 1,000,000,000).
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
Line 1: Two space-separated integers: N and C
Lines 2..N+1: Line i+1 contains an integer stall location, xi
Output
- Line 1: One integer: the largest minimum distance
Sample Input
5 3
1
2
8
4
9
Sample Output
3
Hint
OUTPUT DETAILS:
FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.
Huge input data,scanf is recommended.
题目大意
有N间小屋,第i个小屋在xi的位置。
要让人尽可能远离其它人,问最大能安排多大的距离
解题思路
二分查找可行解
记住二分的模板,就以这题的二分为标准
代码
//POJ2456
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100010;
int s[maxn];
int n,m;
bool ok(int x)
{
int cnt = 1;
int tmp = s[0];
for(int i = 1 ; i < n ; i ++) {
if(s[i]-tmp >= x) {
cnt ++;
tmp = s[i];
}
}
return cnt >= m;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i = 0 ; i < n ; i ++) scanf("%d",&s[i]);
sort(s,s+n);
int ma = s[n-1]-s[0],mi = 0;
while(ma > mi) {
int mid = mi + (ma-mi+1)/2;
if(ok(mid)) mi = mid;
else ma = mid-1;
}
printf("%d\n",mi);
return 0;
}
POJ2456 Aggressive cows 二分的更多相关文章
- 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 ...
随机推荐
- Vue.js+Koa2移动电商实战 笔记
地址:http://jspang.com/ https://github.com/shenghy/SmileVue 1.vant https://www.youzanyun.com/zanui/va ...
- 求阶乘的和(for循环)
第二种方法:
- vue2进阶之v-model在组件上的使用
v-model 用在 input 元素上时 v-model虽然很像使用了双向数据绑定的 Angular 的 ng-model,但是 Vue 是单项数据流,v-model 只是语法糖而已: <in ...
- 保存 laravel model 而不更新 timestamps 的方法
$product = Product::find(1); $product->view_count += 1; $product->timestamps = false; $product ...
- python 全栈开发,Day84(django请求生命周期,FBV和CBV,ORM拾遗,Git)
一.django 请求生命周期 流程图: 1. 当用户在浏览器中输入url时,浏览器会生成请求头和请求体发给服务端请求头和请求体中会包含浏览器的动作(action),这个动作通常为get或者post, ...
- String对象的常用属性和方法
属性 描述 length 在大多数情况下返回字符串中的字符数 方法 描述 toUpperCase() 将字符串修改为大写字母 toLowerCase() 将字符串修改为小写字母 charAt() 以索 ...
- BZOJ1260 [CQOI2007]涂色paint 动态规划
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1260 题意概括 假设你有一条长度为5的木版,初始时没有涂过任何颜色.你希望把它的5个单位长度分别涂 ...
- 打印不同对象的字节表示 ( 对int*强制转换成unsigned char*的理解 )
此文章参考<深入理解计算机系统>P31. 先看如下代码: 12345的十六进制表示为:0x00003039 #include <stdio.h> int main() { ; ...
- [OpenCV-Python] OpenCV 中的图像处理 部分 IV (二)
部分 IVOpenCV 中的图像处理 OpenCV-Python 中文教程(搬运)目录 16 图像平滑 目标 • 学习使用不同的低通滤波器对图像进行模糊 • 使用自定义的滤波器对图像进行卷积(2D 卷 ...
- linux 重要笔记
nginx 服务器重启命令,关闭 nginx -s reload :修改配置后重新加载生效 nginx -s reopen :重新打开日志文件nginx -t -c /path/to/ngin ...