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. Boostrap导航栏跳转到其他页面或外部链接

    想要在boostrap下增加一个标签a,并设置其href属性来实现跳转功能(具体是想在导航栏中添加,点击某个导航栏部件时跳转至其他页面),但是发现事情并不是想象中的那么简单: “Bootstrap为这 ...

  2. 常见字符集&乱码问题

    字符集 常用字符集分类 ASCII及其扩展字符集 作用:表语英语及西欧语言. 位数:ASCII是用7位表示的,能表示128个字符:其扩展使用8位表示,表示256个字符. 范围:ASCII从00到7F, ...

  3. Cordova插件:InAppBrowser

    版权声明:本文为博主原创文章,转载请注明出处 一.应用场景 我们在做cordova app开发的时候,可能会经常遇到这种需求.比如在你的app中,你想打开一个URL,但是又不想跳转到系统的浏览器去打开 ...

  4. 手动增删windows 服务和dll函数

    ①注册windows服务 sc create "服务名AAA" binPath= "安装目录\AAA.exe" displayname= "服务显示名 ...

  5. Golang atomic

    原子操作函数 分为下面系列函数,其中Xxx可以是Int32/Int64/Uint32/Uint64/Uintptr/Pointer其中一种. 1.SwapXxx系列:交换新旧值: // SwapInt ...

  6. Asp.net core 学习笔记 ( Azure key-vault )

    参考 : https://docs.microsoft.com/en-us/aspnet/core/security/key-vault-configuration?view=aspnetcore-2 ...

  7. Asp.net core 学习笔记 ( Area and Feature folder structure 文件结构 )

    2017-09-22 refer : https://msdn.microsoft.com/en-us/magazine/mt763233.aspx?f=255&MSPPError=-2147 ...

  8. nodejs使用log4js记录日志

    log4j是java里面最好用的日志记录插件,在.net上面也有移植log4j.net.同样也移植到了nodejs里面,多的不说,把自测通过的代码粘出来吧. var log4js = require( ...

  9. MyBatis-session-SqlSession

    The SqlSession instance is the most powerful in MyBatis. It is where you'll find all of the methods ...

  10. 雷林鹏分享:jQuery EasyUI 树形菜单 - 使用标记创建树形菜单

    jQuery EasyUI 树形菜单 - 使用标记创建树形菜单 一个树形菜单(Tree)可以从标记创建.easyui 树形菜单(Tree)也可以定义在 元素中.无序列表的 元素提供一个基础的树(Tre ...