poj 2456 Aggressive cows 贪心+二分
Aggressive cows
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 25944 | Accepted: 11982 |
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.
#include<iostream>
#include<algorithm>
#include<math.h>
#define ll long long
using namespace std;
ll a[];
ll n,m;//n是位置数量,m是奶牛数量 int check(ll x)//判断在所有奶牛之中,两两之间的最小距离为x时能否放下所有奶牛
{
ll cnt=,mn=a[];//初始值
for(int i=;i<n;i++)
{
if(a[i]-mn>=x)
{
cnt++;
mn=a[i];//更新
if(cnt==m)
return ;
}
}
return ;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n); ll l=,r=a[n-]-a[],mid;//最小距离x一定在[l,r]之间,采用二分查找方法确定x
while(l<=r)
{
mid=l+(r-l)/;
if(check(mid))//当以mid为最小距离能放下所有奶牛时,再增大mid判断能否放下
l=mid+;
else
r=mid-;
}
printf("%d\n",l- );//因为当l>r时退出while循环,所以结果l-1
}
poj 2456 Aggressive cows 贪心+二分的更多相关文章
- 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(二分+贪心)
题意:把c个牛分进n个摊位,摊位位置已知,所有摊位分布在0 <= xi <= 1,000,000,000,问两头牛间最小距离的最大值. 分析:找所有最小距离取个最大的.所以二分找这个最小的 ...
- 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 题目大意:农夫 建造了一座很长的畜栏,它包括 个隔间,这些小隔间依次编号为. 但是, 的 头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争 ...
- 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 ...
随机推荐
- cookie、 Session Storage 、 Local Storage
问题描述: 使用Ajax, Controller 传回来 JSON 字符串(待处理的信息) 在 Ajax 中实现页面跳转 window.location.href="/jsp/index.j ...
- AVL-Tree (平衡二叉树)
看到网上AVL-Tree大多数都是用相同的实现方式 —— 递归进行插入.删除.维护平衡等,而我比较喜欢用带父指针的数据结构,于是想了一下午,用C实现了一个迭代版的. 由于没有暂时没有好的画二叉树的工具 ...
- Linux进程管理(二)
目录 Linux进程管理(二) 参考 vmstat命令 top命令 Linux进程管理(二)
- vue-mixin
当多个组件需要处理同一个问题,并且处理该问题的逻辑又相似,非常推荐用mixin
- MyBatis 配置文件详解
根元素<configuration>,子元素: <properties> <setttings> <typeAliases> <typeHandl ...
- 124、Java面向对象之引用传递实例二,String类型按值传递
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 微信红包系统设计 & 优化
微信红包系统设计 & 优化 浏览次数:151次 腾讯大讲堂 2015年04月02日 字号: 大 中 小 分享到:QQ空间新浪微博腾讯微博人人网豆瓣网开心网更多0 编者按:经过2014年一年 ...
- App在iTunes Store上的地址
之前可以在电脑上的iTunes上直接搜索到适用于iPhone.iPad的App,进而下载ipa或者复制它的URL. 记得从2017年3月开始就没有这个功能了. ==================== ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:基本的表格
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:表示一个危险动作的按钮操作
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...