POJ - 2456 Aggressive cows 二分 最大化最小值
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 18099 | Accepted: 8619 |
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
#include<stdio.h>
#include<algorithm>
using namespace std; int n, c,x[100005]; int check(int mid)
{
int l = 0;
for (int i = 1; i < c; i++)
{
int r = l + 1;
while (r < n&&x[r] - x[l] < mid)
r++;
if (r == n)return 0;
l = r;
}
return 1;
} int main()
{
while (~scanf("%d %d", &n, &c))
{
for (int i = 0; i < n; i++)
scanf("%d", &x[i]);
sort(x, x + n);
int left = 0, right = x[n-1];
while (right > left +1)
{
int mid = (right + left) / 2;
if (check(mid))
left = mid;
else
right = mid;
}
printf("%d\n", left);
} return 0;
}
POJ - 2456 Aggressive cows 二分 最大化最小值的更多相关文章
- POJ:2456 Aggressive cows(z最大化最小值)
描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1,000, ...
- POJ 2456 Aggressive cows (二分 基础)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7924 Accepted: 3959 D ...
- [POJ] 2456 Aggressive cows (二分查找)
题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...
- POJ 2456 Aggressive cows(二分答案)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...
- 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 ( 二分 && 贪心 )
题意 : 农夫 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 ...
随机推荐
- idea注册码激活防和谐
1.到网站 http://idea.lanyus.com/ 获取注册码: 2.修改hosts文件,位于C:\Windows\System32\drivers\etc,添加一行,win10推荐使用not ...
- 【leetcode 简单】 第九十八题 第三大的数
给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n). 示例 1: 输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 1. 示例 2 ...
- 【译】第十篇 Integration Services:高级事件行为
本篇文章是Integration Services系列的第十篇,详细内容请参考原文. 简介在前一篇, we introduced fault tolerance by examining method ...
- 工具_HBuilder工具使用技巧
https://www.cnblogs.com/xiaohouzai/p/7696152.html
- UNIX环境高级编程 第11章 线程
使用C++调用pthread_cleanup_push( )时,下面的代码是无法编译通过的: pthread_cleanup_push(cleanup, "thread 1 first ha ...
- Android选择头像
http://www.jianshu.com/p/8b3e78046c1c 注意:在Android6.0之后,使用相机拍照需要权限 在选择头像使用相机拍摄时添加以下代码即可. Acp.getInsta ...
- 82.Linux之VMware10.0.4_x64安装
一直想写linux前期软件的一些安装配置的博客,因为中途去弄CORDIC算法了,今天上午刚弄好,除法,乘累加,三角函数等都能达到要求,所以现在来写这块的博客,CORDIC博客就不写了,因为网上很多.V ...
- UBIFS文件系统简介 与 利用mkfs.ubifs和ubinize两个工具制作UBI镜像 (完整理解版本)
UBI文件系统简介 在linux-2.6.27以前,谈到Flash文件系统,大家很多时候多会想到cramfs.jffs2.yaffs2等文件系统. 它们也都是基于文件系 统+mtd+flash设备的架 ...
- 如何开启mysql5.5的客户端服务 命令行打开方法
MySQL分为两个部分,服务器端和客户端,只有服务器端的服务开启后,才可以通过客户端登录到MySQL数据库.这里介绍如何用命令行方式开启mysql的客户端服务. 在计算机上安装好mysql软件 我 ...
- mysql -> 事务&事务锁_09
事务的特性 redo undo 锁的隔离级别