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(二分)的更多相关文章
- poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2456 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...
- [poj 2456] Aggressive cows 二分
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- POJ 2456 Aggressive cows (二分 基础)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7924 Accepted: 3959 D ...
- [POJ] 2456 Aggressive cows (二分查找)
题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...
- 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 ( 二分 && 贪心 )
题意 : 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1e9) ...
- 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 ...
随机推荐
- StringBuilderWriter 这个类需要commons.io.2.6这个包才可以使用, 在maven仓库中搜
- Android 关于虹软人脸识别SDK引擎使用总结
虹软 最近开放了人脸识别的SDK引擎(免费的哦),刚好有Android版的,就体验了一波.下面来说说Android版的SDK使用心得: ArcFace 虹软人脸认知引擎简介 目前开放的版本有人脸比对( ...
- sessionId的生成机制
目录 面试问道这个我居然不知道怎么回答,当然也是因为我确实没有研究过.下面就是百度了一篇文章后简单回答这个问题. 参考:http://www.cnblogs.com/sharpxiajun/p/339 ...
- zlib 2.1.8 编译遇到的问题以及解决方法
环境:win7 x64 + vs2013 1.用vs2013打开zlib-1.2.8\contrib\vstudio\vc11\zlibvc.sln进行编译 包含了下面的的多个项目: miniunz: ...
- Oracle中判断(case when),截取(substr),位置(instr)用法
转自:http://rainbowdesert.iteye.com/blog/1677911 博客分类: SQL 1. 判断(case when) SELECT col1, col2, CASE ...
- gradlew 的https代理设定
在内网编译vlc for Android 时, 总是在 [./gradlew assemble] 卡住, 在网上找到了设置代理的方法: 在gradlew 的同一目录,建立一个 gradle.prope ...
- RHEL 5 , 用安装CD作为YUM的Repository
官方文档写的非常好 14.5. Upgrading the System Off-line with ISO and Yum Create a target directory to mount yo ...
- idea ----> 学习笔记
使用的是社区版的idea 1. 2.关键点之二:配置artifacts.参照 <使用IDEA2017创建java web+Maven项目>http://blog.csdn.net/love ...
- 框架设计——View
[demo]: ZZSZYFP : UserControl, INavigateMdiControl(1)继承自UserControl,并实现了 INavigateMdiControl接口(2)限制表 ...
- 自定义广播(BroadcastReceiver)事件 --Android开发
本例演示自定义广播事件.我们需要做的是,在主活动中写发送广播的代码,然后在接收广播的类中写接收广播的代码. 1.主活动中点击按钮后发送广播 MainActivity.java: public clas ...