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 ...
随机推荐
- Eclipse安卓插件安装
首先说明下载的ADT专门真安卓开发的Eclipse下载下来后就集成了可以直接使用了 但是使用j2EE版本的Eclipse就需要安装插件支持安卓开发了 首先下载ADT Eclipse安卓插件 下载完成后 ...
- just test css
沃尔沃而 public void Commit() { if (_disposed) throw new InvalidOperationException(); if (_transaction = ...
- Android 实现两个list分别出现(在某一时刻只出现一个控件)
第一种方法: 在.xml文件中将这两个List分别放入不同的布局管理器中,比如说 <RelativeLayout android:layout_width="match_parent& ...
- Druid.io SQL乱码问题
1.场景 1.1.依赖版本 avatica-core 1.11.0 druid 0.12.0 1.2.问题重现: 使用Avatica JDBC查询语句:SELECT score FROM studen ...
- C++中的二级指针和指针引用函数传参
在函数的使用过程中,我们都明白传值和传引用会使实参的值发生改变.那么能够通过传指针改变指针所指向的地址吗? 在解决这个问题之前,也许我们应该先了解指针非常容易混淆的三个属性: ①.指针变量地址(&am ...
- Linux基础-软硬连接Block概念
建立/etc/passwd的软连接文件,放在/tmp目录下 使用文件名方式建立的软连接可以跨分区,删除目标文件后,软连接文件失效 建立/etc/passwd的硬链接文件,放在/boot下,如果不成功, ...
- translate 与 相对、绝对定位
垂直水平居中是日常前端开发当中一个常见的需求,在支持 CSS3 属性的现代浏览器当中,有一个利用 CSS3 属性的垂直水平居中方法: position absolute; :; :; :transla ...
- sql_injection之基本get注入
1.代码篇 <?php error_reporting(0); include("../conn.php"); if(isset($_GET['id'])){ $id=$_G ...
- 20165230 2017-2018-2 《Java程序设计》第7周学习总结
20165230 2017-2018-2 <Java程序设计>第7周学习总结 教材学习内容总结 第十一章 JDBC与MySQL数据库 本周了解了如何在Java程序中使用JDBC语提供的AP ...
- 渗透测试===kali linux的安装
方法一: kali linux 安装在本地的vitural box 或者 wm ware中 方法二: 安装在移动硬盘或者储存卡中,插到电脑就能用