最大化最小值 Aggressive cows
Aggressive cows http://poj.org/problem?id=2456
N间小屋,M头牛,使得牛跟牛之间的距离最远,以防止牛打架。
2<=N<=100000
2<=M<=N
0 <=xi<=109
//////////////////////////////////////////////////////////////
C(d):=可以安排牛的位置使得任意两头牛的间距都不小于d
使用二分搜索法解决:
//参考文献:挑战程序设计大赛(第二版)
/*************************************************************************
> File Name: AggressiveCows_poj2456.cpp
> Author: spzhao
> Mail: spzhaol@163.com
> Created Time: 2015年10月14日 星期三 20时25分30秒
************************************************************************/ #include<iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define INF 1000000000
using namespace std;
int N,K;
int x[100005]; bool C(int d)
{
int last = 0;
for (int i = 1;i < K;i++)
{
int crt = last+1; // 只需要比较K-1次找出最适合的值d来放置K头牛,用last & crt 来表示上一头牛和当前牛的位置
while(crt < N && x[crt] - x[last] < d)
crt++;
if (crt == N) return false; // 到达最大值N说明d的值小了
last = crt;
}
return true;
}
int main ()
{
cin >> N >> K;
for (int i = 0;i < N;i++)
scanf("%d",&x[i]);
sort(x,x+N);
int l = 0,r = INF;
while(r - l > 1)
{
int mid = (l+r)/2;
if (C(mid))
l = mid;
else
r = mid;
}
printf("%d\n",l);
return 0;
}
最大化最小值 Aggressive cows的更多相关文章
- 最大化最小值poj2456Aggressive cows
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15528 Accepted: 7440 ...
- poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分
poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...
- 二分法的应用:最大化最小值 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 ...
- 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: 11192 Accepted: 5492 ...
- POJ_2456_Agressive_cows_(二分,最大化最小值)
描述 http://poj.org/problem?id=2456 有n个小屋,线性排列在不同位置,m头牛,每头牛占据一个小屋,求最近的两头牛之间距离的最大值. Aggressive cows Tim ...
- POJ 2456 Aggressive cows ( 二分搜索)
题目链接 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The ...
- POJ2456(最大化最小值)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10728 Accepted: 5288 ...
随机推荐
- doubango(2)--底层协议栈结构分析
tsip_stack_handle_t 实例 1. tsip_stack_handle_t的创建 在底层,真正运转的协议栈结构式tsip_stack_handle_t的一个实例,它的创建 ...
- 在Windows下开发Node.js的C/C++原生扩展
准备工作 (1)本机系统说明:本人机器为win7 64位,32位也可以. (2)软件安装: VISUAL C++ 2010 EXPRESS(Visual Studio 2010也可以): window ...
- HDU1864(背包)
最大报销额 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- android Fragment和FragmentActivity
MainActivity.java import android.app.AlertDialog; import android.app.Notification; import android.co ...
- 在VMWare虚拟机中安装Ubuntu 16.04.1 LTS
一.需要的准备 安装好VMWare虚拟机(傻瓜式安装,一直next就可以,请支持正版),将Ubuntu的系统镜像下载好,目前最新的LTS版本为16.04.1. 我把虚拟机和Ubuntu镜像传到了百度云 ...
- asp.net权限认证:OWIN实现OAuth 2.0 之客户端模式(Client Credential)
asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...
- SQL中如何使用EXISTS替代IN
原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6385312.html 我们在程序中一般在做SQL优化的时候讲究使用EXISTS带替代IN的做 ...
- 测试指南(适用于Feature/promotion/bug)
1.提前了解需求,在需求的业务基础和开发的架构基础上分析测试关键点,给出测试策略,甚至需要准备测试数据: 2.分析需求时不要受开发影响,要有自己的分析和判断,包括测试范围,测试时间: 3.在开始测试之 ...
- Code forces 719A Vitya in the Countryside
A. Vitya in the Countryside time limit per test:1 second memory limit per test:256 megabytes input:s ...
- 欢迎来到Devil_lixu的技术博客
Welcome to my blog 如果你觉得内容对你还有一点点的帮助,那就果断点赞吧^_^