POJ 2456 Aggressive cows (二分 基础)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 7924 | Accepted: 3959 |
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
Hint
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.
Source
题目链接:
id=2456">http://poj.org/problem?
id=2456
题目大意:一个数轴上n个点,每一个点一个整数值,有c个物品。要放在这些点的某几个上,求怎么放能够使随意两个物品间距离的最小值最大,求这个最大值
题目分析:最小值最大,典型二分题。二分距离的值推断
#include <cstdio>
#include <algorithm>
using namespace std;
int const INF = 0x3fffffff;
int const MAX = 1e5 + 5;
int d[MAX];
int n, c; int cal(int m)
{
int ans = 0, now = d[0];
for(int i = 1; i < n; )
{
while(d[i] < now + m)
i ++;
ans ++;
now = d[i];
}
return ans;
} int main()
{
scanf("%d %d", &n, &c);
for(int i = 0; i < n; i++)
scanf("%d", &d[i]);
sort(d, d + n);
int r = d[n - 1] - d[0], l = 0, ans = 0;
while(l <= r)
{
int m = (l + r) / 2;
int num = cal(m);
if(num >= c)
{
ans = m;
l = m + 1;
}
else
r = m - 1;
}
printf("%d\n", ans);
}
POJ 2456 Aggressive cows (二分 基础)的更多相关文章
- POJ 2456 Aggressive cows(二分答案)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...
- POJ - 2456 Aggressive cows 二分 最大化最小值
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18099 Accepted: 8619 ...
- poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2456 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...
- [poj 2456] Aggressive cows 二分
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- [POJ] 2456 Aggressive cows (二分查找)
题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...
- POJ 2456 Aggressive cows ( 二分 && 贪心 )
题意 : 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1e9) ...
- poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分
poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...
- 二分搜索 POJ 2456 Aggressive cows
题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...
- POJ 2456 Agressive cows(二分)
POJ 2456 Agressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2≤N≤100,000)个隔间,这 些小隔间的位置为x0,...,xN-1 (0≤xi≤1,000,0 ...
随机推荐
- react-router 4.0版本学习笔记
Router 所有路由组件的底层接口,一般情况都不使用,而是使用更加高级的路由. 最常用的有两种<BrowserRouter>.<HashRouter> <Browser ...
- Push pull, open drain circuit, pull up, pull down resistor
Push pull 就以下面這個 電路來說, 因為沒有 pull up resistor, 所以 output voltage 由 low 往 high 的速度會較快. 有兩個電晶體,一個on,一個 ...
- JavaScript 之类型转换
数值转换成字符串类型 1.利用 “+” 将数值加上一个长度为零的空字符串. 2.通过toString()方法.加入参数可以直接进行进制的转换. <script language="ja ...
- Python Challenge 第九关
第九关只有一幅图,上面有一些黑点.网页名字叫:connect the dots.可能是要把这些点连起来. 查看源代码,果然有两个整数集合 first 和 second.并且有个提示:first+sec ...
- 【原创】设置EXCEL2010打开多个独立窗口
最近发现一个奇怪的问题,发现office中的word和ppt在我使用笔记本分屏幕(双屏)的时候都可以将2份文档分别在2个窗口打开,但是在使用excel的时候却发现不行,最后研究发现原因 ...
- AC日记——[HAOI2007]理想的正方形 P2216
[HAOI2007] 理想的正方形 思路: 正解多个单调队列: 但是我用树套树水了过去: 来,上代码: #include <cstdio> #include <cstring> ...
- github如何实现fork的项目与原项目同步
refer to https://www.jianshu.com/p/fede3333205f 作者:hitchc 链接:https://www.jianshu.com/p/fede3333205f ...
- Jenkins错误“to depth infinity with ignoreexternals:true”问题解决
试下以下解决方法: 1.可能是SVN插件版本过低导致,升级SVN插件. 2.可能是构建时自己手动修改了代码,而SVN检出时无法覆盖导致的错误,可以先删除jenkins检出的代码,然后再检出一次去构建. ...
- flask的日志输出current_app.logger.debug
环境部署方式:nginx+supervisord+gunicorn在/etc/supervisord.conf中配置日志的输出路径stdout_logfile=/home/admin/workspac ...
- C++中的void类型
Technorati 标签: void,指针 1.1. void类型 void类型其实是一种用于语法性的类型,而不是数据类型,主要用于作为函数的参数或返回值,或者定义void指针,表示一种未知类型. ...