[poj 2456] Aggressive cows 二分
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.
题意:
又见农场主John.. 他有C头牛,N个牛栏排成一条直线,每个的位置为Xi,他的牛比较好斗,把C头牛放到N个牛栏里两两之间要离得尽可能的远,问求这个最大的最小距离。
思路:
求最大最小距离,可以从大到小枚举可能的最小距离,如果能把C头牛都放下,则这个距离就是最大最小距离,否则继续减小枚举距离。但是这样时间复杂的也太大了,考虑采用二分来找距离,L = 0, R = 1000000010, mid = (L+R)/2, 如果mid这个距离可以的话,可能有更大的距离满足,因此在[mid, R]中寻找,如果不可以需要缩小范围,在[L, mid)中寻找,如此进行下去,最终到R-L=1是停止,在之前mid是可以的,L = mid 最后输出L即可。
对于搜寻距离d是否可行时,由贪心1是肯定要选的,然后在找C-1头牛的位置,last为前一个的指针,cur为当前指针,找到cur的第一个位置,如果超出范围了就不可以, 全部找完了此种方案可行。
代码:
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
int a[];
int N, C; bool judge(int d)
{
int last = , cur;
for (int i = ; i < C; i++) {
cur = last + ;
while (cur < N && a[cur]-a[last]<d)
cur++;
if (cur == N) return ;
last = cur;
}
return ;
} int main()
{
//freopen("1.txt", "r", stdin);
scanf("%d%d", &N, &C);
for (int i = ; i < N; i++) {
scanf("%d", &a[i]);
}
sort(a, a+N);
int L = , R = ;
while (R - L > ) {
int mid = (L+R)/;
if (judge(mid))
L = mid;
else
R = mid;
}
printf("%d\n", L); return ;
}
[poj 2456] Aggressive cows 二分的更多相关文章
- POJ 2456 Aggressive cows (二分 基础)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7924 Accepted: 3959 D ...
- 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 (二分查找)
题目地址: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 ...
随机推荐
- Azure VMSS ---- PowerShell创建自定义镜像的VMSS集群
前面一篇文章介绍了如何用PowerShell创建标准镜像的VMSS集群.http://www.cnblogs.com/hengwei/p/7391178.html 本文将介绍,如何用PowerShel ...
- UVA548(二叉树遍历)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- python selenium 滚动条处理、页面拖动
selenium中没有直接控制滚动条的方法,可以使用方法:execute_script(),可以直接执行js的脚本. 一.竖向滚动条控制,三种方法总有一款适合你. 1.滚动条拉到最底: js=&quo ...
- JS和OC间的通信(使用JavaScriptCore)
JavaScriptCore 时代的通讯 iOS 7 开始,苹果提供了一个叫作 JavaScriptCore 的框架,使用 JavaScriptCore 框架可以实现 OC 和 JS 的互相调用,而不 ...
- c语言-构建一个静态二叉树
第一.树的构建 定义树结构 struct BTNode { char data; struct BTNode* pLChild; struct BTNode* pRChild; }; 静态方式创建一个 ...
- Celery-4.1 用户指南: Signals (信号)
基础 有多种类型的事件可以触发信号,你可以连接到这些信号,使得在他们触发的时候执行操作. 连接到 after_task_publish 信号的示例: from celery.signals impor ...
- 问题:iis配置json;结果:如何配置iis支持.json格式的文件
如何配置iis支持.json格式的文件 | 浏览:1357 | 更新:2015-04-05 11:00 | 标签:软件 1 2 3 4 5 6 7 分步阅读 现在大家在制作HTM5的一些小场景,小游戏 ...
- 如何实现1080P延迟低于500ms的实时超清直播传输技术<转>
转载地址:http://www.yunweipai.com/archives/9037.html 最近由于公司业务关系,需要一个在公网上能实时互动超清视频的架构和技术方案.众所周知,视频直播用 CDN ...
- x264的一些参数设置对编码效率的影响
i_luma_deadzone[0]和i_luma_deadzone[1]分别对应inter和intra, 取值范围1~32,测试可以得知,这连个参数的调整可以对数据量有很大影响,值越大数据量相应越少 ...
- 【总结整理】WebGIS学习-thinkGIS(三):关于影像金字塔、瓦片行列号、分辨率resolution
http://www.thinkgis.cn/topic/541a5206da8db186fd0673ba 1.前言 在上一节中我们知道了屏幕上一像素等于实际中多少单位长度(米或经纬度)的换算方法,而 ...