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 ...
随机推荐
- async、await在ASP.NET[ MVC]中之线程死锁的故事
场景重构 public ActionResult Index(string ucode) { string userInfo = GetUserInfo(ucode).Result; ViewData ...
- Codeforces 931F - Teodor is not a liar!
931F - Teodor is not a liar! 思路: 最长上升子序列 先差分数组染色 如果存在一个点,被所有区间包含,那么这张图一定是山峰状,如下图: 那么只要分别从前和从后找一个最长非严 ...
- Jmeter 接口测试知识梳理——应用基础篇
Jmeter 使用也有很长时间了,但是一直没有做一下知识梳理,近期会对公司同事做一下这方面的培训,借此机会,把使用过程中应用到的知识,或是遇到的问题,整理出来,方便大家学习! Jmeter 接口测试知 ...
- java再次学习
1.maven配置.
- Unity--- 纹理设置属性 alphaIsTransparency
官方的解释: 意思就是没什么实际效果,只是用做显示用. 参考:https://docs.unity3d.com/ScriptReference/Texture2D-alphaIsTransparenc ...
- <property name="hibernate.hbm2ddl.auto">update</property> 问题
其实这个hibernate.hbm2ddl.auto参数的作用主要用于:自动创建|更新|验证数据库表结构.如果不是此方面的需求建议set value="none".create:每 ...
- VMware vSphere client 中英文语言界面设置
安装的时候可以选择简体中文,然后安装后,对vsphere client的执行程序制作快捷方式 以win7 x64环境默认路径为例 英文启动如下: "C:Program Files (x86) ...
- Python核心编程的四大神兽
http://www.cnblogs.com/ssy3340/p/9747722.html
- English trip EM2-LP-1A Hi Teacher:Taylor
课上内容(Lesson) 词汇(Key Word ) Introduce vt. 介绍:引进:提出:采用 greet [ɡrit] vt. 欢迎,迎接:致敬,致意:映入眼帘 n. (Greet ...
- VS Code行内样式提示插件
打开vscode,在软件界面左下角找到“齿轮”标志并点击,在弹出的菜单中选择“设置”,把下面的代码添加到设置里. { "workbench.colorTheme": "C ...