POJ2456 Aggressive cows 二分
来源:互联网 发布:mirror软件官网 编辑:程序博客网 时间:2024/05/18 00:43
Description
Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,…,xN (0 <= xi <= 1,000,000,000).
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
Line 1: Two space-separated integers: N and C
Lines 2..N+1: Line i+1 contains an integer stall location, xi
Output
- Line 1: One integer: the largest minimum distance
Sample Input
5 3
1
2
8
4
9
Sample Output
3
Hint
OUTPUT DETAILS:
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.
题目大意
有N间小屋,第i个小屋在xi的位置。要让人尽可能远离其他人,问最大能安排多大的距离
解题思路
二分查找可行解
记住二分的模板,就以这题的二分为标准
代码
//POJ2456#include <cstdio>#include <algorithm>using namespace std;const int maxn = 100010;int s[maxn];int n,m;bool ok(int x){ int cnt = 1; int tmp = s[0]; for(int i = 1 ; i < n ; i ++) { if(s[i]-tmp >= x) { cnt ++; tmp = s[i]; } } return cnt >= m;}int main(){ scanf("%d%d",&n,&m); for(int i = 0 ; i < n ; i ++) scanf("%d",&s[i]); sort(s,s+n); int ma = s[n-1]-s[0],mi = 0; while(ma > mi) { int mid = mi + (ma-mi+1)/2; if(ok(mid)) mi = mid; else ma = mid-1; } printf("%d\n",mi); return 0;}
- POJ2456 Aggressive cows (二分)
- POJ2456 Aggressive cows 二分
- POJ2456 Aggressive cows(二分+贪心)
- POJ2456 Aggressive cows 二分+判断
- POJ2456 -- Aggressive cows(二分搜索)
- POJ2456 Aggressive cows(二分搜索)
- POJ2456 Aggressive cows(对整数二分)
- poj2456-Aggressive cows(nyoj586)【贪心-二分】
- POJ2456 Aggressive cows 最大值最小化(二分)
- POJ2456(Aggressive cows)(贪心+二分)
- POJ2456 Aggressive cows
- POJ2456 Aggressive cows
- POJ2456-Aggressive cows
- POJ2456 Aggressive cows
- POJ2456 Aggressive cows
- POJ2456-Aggressive cows
- poj2456 Aggressive cows
- codevs 2982 Aggressive cows愤怒的奶牛poj2456--二分答案
- JavaScript基础算法
- OpenStack Juno安装---网络配置
- Redis中常用命令
- 详解boost库中的Message Queue
- 看好你的门-攻击服务端(6)-对SMTP注入攻击 操纵邮件发送
- POJ2456 Aggressive cows 二分
- 中缀转后缀并计算
- java多线程的总结(一)
- jQuery性能优化大全
- [leetcode160链表]Intersection of Two Linked Lists
- Maven生命周期
- Android推送
- Quartz作业调度框架
- 【免费直播课】AppCan基础课程之第五讲 UI控件