Aggressive Cows 二分
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<cstdio>
using namespace std;
#define MAXN 100001
#define INF 1000000001
/*
将奶牛分配到到多个点,使得他们之间的最小距离最大
check(x)函数 最小距离为x的时候能否放下n头奶牛
*/
int n, c, a[MAXN];
bool check(int x)
{
int tmp = a[], cnt = ;
for(int i=;i<n;i++)
{
if (a[i] - tmp >= x)
{
cnt++;
tmp = a[i];
}
}
return (cnt >= c);
}
int main()
{
while (scanf("%d%d", &n, &c) != EOF)
{
int Max = -;
for (int i = ; i < n; i++)
{
scanf("%d", &a[i]);
Max = max(Max, a[i]);
}
int beg = , end = Max,ans=;
sort(a, a + n);
while (beg <= end)
{
int mid = (beg + end) / ;
if (check(mid))
{
ans = mid;
beg = mid + ;
}
else
end = mid - ;
}
printf("%d\n", ans);
}
}
Aggressive Cows 二分的更多相关文章
- 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: 7924 Accepted: 3959 D ...
- POJ2456 Aggressive cows 二分
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- [poj 2456] Aggressive cows 二分
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- Aggressive cows 二分不仅仅是查找
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7083 Accepted: 3522 Description Farme ...
- [POJ] 2456 Aggressive cows (二分查找)
题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...
- POJ2456 Aggressive cows(二分+贪心)
如果C(d)为满足全部牛之间的距离都不小于d. 先对牛舍的位置排序,然后二分枚举d,寻找满足条件的d. #include<iostream> #include<cstdio> ...
- poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2456 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...
随机推荐
- java replaceAll 忽略大小写
public static void main(String[] args) { String temp=" CLASS_path : /aaabb/"; System.out.p ...
- [Swift通天遁地]四、网络和线程-(3)线程组:使用DispatchGroup(调度组)对线程进行分组管理
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- sigar的使用
与普通jar包不同,Sigar API还要依赖本地的库文件来进行工作,其中: Windows下Sigar.jar 依赖:sigar-amd64-winnt.dll 或 sigar-x86-winnt. ...
- CyclibcBarrier与CountDownLatch区别
1.CyclibcBarrier的线程运行到某个位置后即停止运行,直到所有的线程都到达这个点,所有线程才开始运行:CountDownLatch是线程运行到某个点后,计数器-1,程序继续运行即Cycli ...
- longpo的回文
啊...比赛的时候输入打错了,结束之后还照着题解把DP部分重构了一遍然而还是WA...样例都没过,然后直接输了-1 明显的DP...而且数据范围这么小,显然怎么搞都可以... 而且这样的回文的DP是很 ...
- Oracle 10g RAC的负载均衡配置[转载]
Oracle 10g RAC的负载均衡配置 负载均衡是指连接的负载均衡.RAC的负载均衡主要是指新会话连接到RAC数据库时,如何判定这个新的连接要连到哪个节点进行工作.在RAC中,负载均衡分为两种,一 ...
- C# 利用反射进行类型转换
/// <summary> /// 父类转子类 /// </summary> /// <typeparam name="TParent">< ...
- 软件图标显示不正常【win7企业版】
现象: 原因: 图标缓存没有把该软件图标建立起来 解决: 一. 1.找到 IconCache.db 2.你要把电脑隐藏文件打开不然找不到这个文件的,组织—文件夹及搜索选项——查看——显示隐藏文件.文件 ...
- MVC系列学习(四)-初识Asp.NetMVC框架
注:本文章从伯乐那盗了两张图,和一些文字: 1.MVC设计模式 与 Asp.Net Mvc框架 a.MVC设计模式 MVC设计模式 是一种 软件设计模式,将业务逻辑 与 界面显示 分离,并通过某种方式 ...
- 鼠标单击到 img行的时候图片隐藏方案
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...