poj 2456 Aggressive cows (二分查找)
来源:互联网 发布:nginx 代理静态页面 编辑:程序博客网 时间:2024/05/29 18:01
Aggressive cows
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 15921 Accepted: 7625
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?
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
* 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 312849
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.
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.
Source
USACO 2005 February Gold
解题思路:
这道题虽然不长,可是读了好几遍才读懂。有n个牛舍,每个牛舍有自己的位置xi(可以理解为坐标),把c头牛放在这n个牛舍里面,要求最近的两头牛之间的最大距离。
比如测试数据 5个牛舍 3头牛 牛舍坐标1 2 8 4 9
先按贪心,第一头牛肯定被放在第一个牛舍里,排序 1 2 4 8 9
第二头牛可以放在2,也可以放在4,假设第二头牛放在2 ,与第一头牛的距离为1 ,假设第三头放在9,与第二头的距离为7,那么,最近的两头牛之间的最大距离为1
最有解为 第一头牛放在1 第二头牛放在4 ,第三头牛放在9 ,那么 4-1=3 9-4=5 ,最近的两头牛之间的最大距离为3
也就是求相邻两头牛之间距离的最小值(取最大)。这个距离采用二分的形式进行判断。
#include<stdio.h>#include<string.h>#include<math.h>#include<string>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<list>#include<map>#include<set>using namespace std;const int N = 100010;const int INF = 1e9 + 100;int n,C;int a[N];bool OK( int x ){ int cnt = 1 ; int fi = 0 , se = 0; while( fi < n ){ se = fi; fi = lower_bound( a , a + n, a[se] + x )-a ; if( fi < n ) cnt ++ ; } if(cnt >= C ) return 1; return 0;}void work(){ sort(a , a + n); int st = 0 , ed = INF , mid ; while( st + 1 < ed ){ mid = ( st + ed ) >> 1 ; if(OK( mid )) st = mid; else ed = mid; } printf("%d\n",st);}int main(){//freopen("in.in","r",stdin); while(~scanf("%d%d",&n,&C)){ for(int i = 0 ; i < n ; i ++) scanf("%d", &a[i]); work(); }return 0;}
阅读全文
0 0
- POJ - 2456 Aggressive cows(二分查找)
- poj 2456 Aggressive cows (二分查找)
- POJ 2456 Aggressive cows (二分查找)
- poj 2456 Aggressive cows (二分查找)
- [POJ](2456)Aggressive cows ---二分查找+贪心(查找+贪心)
- poj 2456 Aggressive cows(二分查找)
- POJ 2456 Aggressive cows 二分查找
- [ACM] poj 2456 Aggressive cows (二分查找)
- POJ 2456 Aggressive cows(二分查找 最大化最小值)
- POJ 2456 Aggressive cows(二分查找最大化最小值)
- POJ 2456 Aggressive cows(二分)
- POJ 2456 - Aggressive cows(二分)
- poj 2456 Aggressive cows(贪心+二分)
- poj 2456 Aggressive cows(贪心+二分)
- POJ 2456 Aggressive cows(二分)
- POJ 2456 Aggressive cows (二分搜索)
- POJ 2456 Aggressive cows(二分)
- POJ 2456 Aggressive cows (贪心 + 二分)
- [UnityUI]使用UI框架
- 关机/重启命令
- HTML简介
- 等待事件:enq: HW
- vmware12安装教程
- poj 2456 Aggressive cows (二分查找)
- 【Uniy3d之EnhanceScollView选择角色3D循环滚动效果实现】
- Linux文件权限基础
- 文章标题
- Linux网络编程--sendfile零拷贝高效率发送文件
- 第一个struts2服务
- Linux的好习惯
- SSH笔记: struct中使用过滤器
- 第三周项目四C/C++顺序表的应用