POJ 2456 Agressive cows

农夫 John 建造了一座很长的畜栏,它包括N (2≤N≤100,000)个隔间,这 些小隔间的位置为x0,...,xN-1 (0≤xi≤1,000,000,000,均为整数,各不相同).

John的C (2≤C≤N)头牛每头分到一个隔间。牛都希望互相离得远点省得 互相打扰。怎样才能使任意两头牛之间的最小距离尽可能的大,这个最 大的最小距离是多少呢

思想:二分,首先把输入的数据进行从小到大排序,再由最短距离为1,最长距离为(a[n-1] - a[0] + 1 - c) / (c - 1) + 1进行二分测试即可

代码:

#include<iostream>
#include<algorithm>
using namespace std;
#define N 100000+5
int a[N];
int main() {
int n, c;
scanf("%d%d", &n, &c);
for(int i = ; i < n; i++) scanf("%d", &a[i]);
sort(a, a+n);
int L = , R = (a[n-] - a[] + - c)/(c-) + ;
int mid, ans;
while(L <= R) {
mid = L + (R - L)/;
int i = , count = , pre = ;
while(i < n && count < c) {
while(i < n && a[i] - a[pre] < mid) i++;
if(i < n) count++;
pre = i;
i++;
}
if(count < c) {
R = mid - ;
} else {
ans = mid;
L = mid + ;
}
}
printf("%d\n", ans);
return ;
}

POJ 2456 Agressive cows(二分)的更多相关文章

  1. poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2456 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...

  2. [poj 2456] Aggressive cows 二分

    Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...

  3. POJ 2456 Aggressive cows (二分 基础)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7924   Accepted: 3959 D ...

  4. [POJ] 2456 Aggressive cows (二分查找)

    题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...

  5. POJ 2456 Aggressive cows(二分答案)

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...

  6. POJ - 2456 Aggressive cows 二分 最大化最小值

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18099   Accepted: 8619 ...

  7. POJ 2456 Aggressive cows ( 二分 && 贪心 )

    题意 : 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1e9) ...

  8. poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分

    poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...

  9. 二分搜索 POJ 2456 Aggressive cows

    题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...

随机推荐

  1. MySql登陆密码忘记了怎么办?MySQL重置root密码方法

    本文主要介绍Windows和Linux系统下忘记密码重置root密码的方法,需要的朋友可以参考下. MySQL有时候忘记了root密码是一件伤感的事.这里提供Windows 和 Linux 下的密码重 ...

  2. python return dict bug?

    def to_dict(self): para = OrdererDict() para['dd'] = self.XXX ... return para 这样一个简单函数 para1 = obj.t ...

  3. Spring Cloud 入门教程(四): 分布式环境下自动发现配置服务

    前一章, 我们的Hello world应用服务,通过配置服务器Config Server获取到了我们配置的hello信息“hello world”. 但自己的配置文件中必须配置config serve ...

  4. Codeforces 1005 F - Berland and the Shortest Paths

    F - Berland and the Shortest Paths 思路: bfs+dfs 首先,bfs找出1到其他点的最短路径大小dis[i] 然后对于2...n中的每个节点u,找到它所能改变的所 ...

  5. Asp.net core 学习笔记 ( ef core )

    更新 : 2018-11-26 这里记入一下关于 foreignKey cascade action 默认情况下如果我们使用 data annotation required + foreginkey ...

  6. (转)C# Stopwatch详解

    C# Stopwatch类 命名空间:System.Diagnostics; namespace System.Diagnostics { // 提供一组方法和属性,可用于准确地测量运行时间. pub ...

  7. spring cloud: 关闭ribbon负载均衡

    spring cloud: 关闭ribbon负载均衡 1.eureka服务 2.2个user服务:7900/7901 3,movie服务 movie服务去请求 user的用户信息,而此时只想请求790 ...

  8. 雷林鹏分享:XML 实例

    XML 实例 这些例子演示了 XML 文件.XML 格式化和 XML 转换(XSLT). 还演示了与 XML 一起使用的 JavaScript(AJAX). 查看 XML 文件 查看一个简单的 XML ...

  9. 【转】 g++编译时对'xxxx'未定义的引用问题(undefined reference to)

    转自:https://blog.csdn.net/killwho/article/details/53785910 引用:http://www.linuxdiyf.com/linux/16754.ht ...

  10. 中心极限定理 | central limit theorem | 大数定律 | law of large numbers

    每个大学教材上都会提到这个定理,枯燥地给出了定义和公式,并没有解释来龙去脉,导致大多数人望而生畏,并没有理解它的美. <女士品茶>有感 待续~ 参考:怎样理解和区分中心极限定理与大数定律?