Aggressive cows(POJ 2456)
- 原题如下:
Aggressive cows
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20524 Accepted: 9740 Description
Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000).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
* Line 1: Two space-separated integers: N and C* Lines 2..N+1: Line i+1 contains an integer stall location, xi
Output
* Line 1: One integer: the largest minimum distanceSample Input
5 3
1
2
8
4
9Sample Output
3
Hint
OUTPUT DETAILS: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.
- 题解:类似的最大化最小值或者最小化最大值的问题,通常用二分搜索法解决。
我们定义:C(d):=可以安排牛的位置使得最近的两头牛的距离不小于d,那么问题就变成了求满足C(d)的最大的d。另外,最近的间距不小于d也可以说成是所有牛的间距都不小于d,因此就有C(d)=可以安排牛的位置使得任意的牛的间距都不小于d,这个问题的判断使用贪心法就可以解决。 - 代码:
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cmath>
#define num s-'0' using namespace std; const int MAX_N=;
const int INF=0x3f3f3f3f;
int N,M;
int x[MAX_N]; void read(int &x){
char s;
x=;
bool flag=;
while(!isdigit(s=getchar()))
(s=='-')&&(flag=true);
for(x=num;isdigit(s=getchar());x=x*+num);
(flag)&&(x=-x);
} void write(int x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
write(x/);
putchar(x%+'');
} bool C(int); int main()
{
read(N);read(M);
for (int i=; i<N; i++) read(x[i]);
sort(x, x+N);
int lb=, ub=INF;
while (ub-lb>)
{
int mid=(lb+ub)/;
if (C(mid)) lb=mid;
else ub=mid;
}
write(lb);
putchar('\n');
} bool C(int d)
{
int last=;
for (int i=; i<M; i++)
{
int crt=last+;
while (crt<N && x[crt]-x[last]<d) crt++;
if (crt==N) return false;
last=crt;
}
return true;
}
Aggressive cows(POJ 2456)的更多相关文章
- Divide and conquer:Aggressive Cows(POJ 2456)
侵略性的牛 题目大意:C头牛最大化他们的最短距离 常规题,二分法即可 #include <iostream> #include <algorithm> #include < ...
- 【算法】题目分析:Aggressive Cow (POJ 2456)
题目信息 作者:不详 链接:http://poj.org/problem?id=2456 来源:PKU JudgeOnline Aggressive cows[1] Time Limit: 1000M ...
- poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分
poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...
- POJ 2456 Aggressive cows (二分 基础)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7924 Accepted: 3959 D ...
- 二分搜索 POJ 2456 Aggressive cows
题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...
- 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: 11192 Accepted: 5492 ...
- POJ 2456: Aggressive cows(二分,贪心)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20485 Accepted: 9719 ...
随机推荐
- 提前批笔试一道算法题的Java实现
题目描述 这是2021广联达校招提前批笔试算法题之一. 我们希望一个序列中的元素是各不相同的,但是理想和显示往往是有差距的.现在给出一个序列A,其中难免有相同的元素,现在提供了一种变化方式,使得经过若 ...
- ubuntu18.04配置与美化
一:初步系统配置 1 不可或缺的更新 如果在上一步中勾选了安装 Ubuntu 时下载更新,那么大部分的系统更新已经下载完毕. 不过为了确保,先移步到 设置→详细信息 ,点击右下角的 检查更新 ,如果存 ...
- MySQL空间函数实现位置打卡
项目需求是跟用户当前位置判断是否在给定的地理位置范围内,符合位置限制才可以打卡,其中的位置范围是一个或多个不规则的多边形.如下图,判断用户是在清华还是北大. 图形获取区域坐标 因为项目前端使用微信小程 ...
- SVN的基本使用
2020年7月6日 为什么需要版本控制? 需要清晰地保存某些文件的不同修订版本 控制文件的发屐过程,找出导致 BUG 的原因 轻松将项目或文件恢复到指定版本 极大方便团队之间协同开发,防止出现混乱 在 ...
- CSS动画实例:太极图在旋转
利用CSS可以构造出图形,然后可以对构造的图形添加动画效果.下面我们通过旋转的太极图.放大的五角星.跳“双人舞”的弯月等实例来体会纯CSS实现动画的方法. 1.旋转的太极图 设页面中有<div ...
- 分析dubbo心跳检测机制
目的: 维持provider和consumer之间的长连接 实现: dubbo心跳时间heartbeat默认是60s,超过heartbeat时间没有收到消息,就发送心跳消息(provider,cons ...
- @SuppressWarnings注解用法详解(转)
原文连接https://blog.csdn.net/sysware_carol/article/details/52100580 今天来谈谈@SuppressWarnings注解的作用. J2SE 提 ...
- golang并发
1.goroutine goroutine是Go并行设计的核心.goroutine说到底其实就是线程,但是它比线程更小,十几个goroutine可能体现在底层就是五六个线程,Go语言内部帮你实现了这些 ...
- 使用分区挂载 ftp 目录
ftp挂载分区上去后无法识别的问题困扰了我好几天,今天有时间把它研究了一下,拿出来与大家分享一下. ftp目录挂载分区前与挂载分区后的区别[root@localhost ~]# ls -Zd /var ...
- 洛谷T90444 密码 题解
[问题描述] 假发通过了不懈的努力,得到了将军家门锁的密码(一串小写英文字母).但是假发被十四和猩猩他们盯上了,所以假发需要把密码传递出去.因为假发不想十四他们发现几松门前贴的小纸条就是将军家的密码, ...