POJ 2456 Aggressive cows
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 11192 | Accepted: 5492 |
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 <cstdio>
#include <algorithm>
using namespace std; const int MAXN = 100000+5;
int a[MAXN];
int n, c; bool ok(int d)
{
int pre = 0;
for(int i = 1; i < c; ++i){
int cur = pre+1;
while(cur < n && a[cur]-a[pre] < d)
++cur;
if(cur == n)
return false;
pre = cur;
}
return true;
} int main()
{
scanf("%d%d", &n, &c);
for(int i = 0; i < n; ++i)
scanf("%d", &a[i]);
sort(a, a+n);
int l = 0, r = 0x7fffffff;
while(r-l > 1){
int mid = (l+r)/2;
if(ok(mid)) l = mid;
else r = mid;
}
printf("%d\n", l);
return 0;
}
POJ 2456 Aggressive cows的更多相关文章
- 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 Aggressive cows (二分 基础)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7924 Accepted: 3959 D ...
- POJ 2456 Aggressive cows (二分)
题目传送门 POJ 2456 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) s ...
- [ACM] poj 2456 Aggressive cows (二分查找)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5436 Accepted: 2720 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 贪心+二分
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25944 Accepted: 11982 ...
- POJ 2456 Aggressive cows(贪心 + 二分)
原题链接:Aggressive cows 题目大意:农夫 建造了一座很长的畜栏,它包括 个隔间,这些小隔间依次编号为. 但是, 的 头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争 ...
随机推荐
- C++中的const关键字
http://blog.csdn.net/eric_jo/article/details/4138548 C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方 ...
- Eclipse Error: Unable to set localhost. This prevents creation of a GUID.
Symptoms The following error appears in the atlassian-confluence.log: 2011-03-16 18:20:03,021 ERROR ...
- 【leetcode】Median of Two Sorted Arrays(hard)★!!
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- sublime 复制黏贴等快捷键修改
在 keyboard-binding user 里 增加个人配置来覆盖默认配置 [ { "keys": ["ctrl+z"], "command&qu ...
- 【Linux高频命令专题(16)】less
概述 less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向 ...
- SDK 与MFC
SDK 就是Software Development Kit 软件开发包MFC 就是Microsoft Foundation Classes 微软函数类库.是以C++类的形式封装了Windows的AP ...
- swift学习笔记-UI篇之UIImageView
1.基本使用 将要使用的图片拖入到项目里,我这里使用的是名为“1.jpg”的图片,然后创建UIImageView,并设置要显示的图片为"1.jpg"//1. 基本使用 let im ...
- 一行很好的JS代码
[].forEach.call($$("*"),function(a){ a.style.outline="1px solid #"+(~~(Math.rand ...
- 如何用Maven创建一个普通Java项目
一下内容包括:用Maven创建一个普通Java项目,并把该项目转成IDEA项目,导入到IDEA,最后把这个项目打包成一个jar文件. 有时候运行mvn命令失败,重复运行几次就OK了,无解(可能因为网络 ...
- 受限波兹曼机导论Introduction to Restricted Boltzmann Machines
Suppose you ask a bunch of users to rate a set of movies on a 0-100 scale. In classical factor analy ...