POJ_2456_Agressive_cows_(二分,最大化最小值)
2016-04-22 23:12
351 查看
描述
http://poj.org/problem?id=2456
有n个小屋,线性排列在不同位置,m头牛,每头牛占据一个小屋,求最近的两头牛之间距离的最大值.
Aggressive cows
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10095 | Accepted: 4997 |
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 distanceSample 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.
Source
USACO 2005 February Gold分析
二分.
最大化最小值.
C(x)表示使两头牛之间最小距离为x,问题转化为求满足C(x)的x的最大值.可知x<=(总长)/(c-1).那么1~(总长)/(c-1)二分求解即可.
#include<cstdio> #include<algorithm> using std :: sort; const int maxn=100005; int n,c; int a[maxn]; void init() { scanf("%d%d",&n,&c); for(int i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+n+1); } bool judge(int x) { int last=1; for(int i=2;i<=c;i++) { int now=last+1; while(now<=n&&(a[now]-a[last]<x)) now++; if(now>n) return false; last=now; } return true; } int bsearch(int x,int y) { while(x<y) { int m=x+(y-x+1)/2; if(judge(m)) x=m; else y=m-1; } return x; } void solve() { int INF=(a -a[1])/(c-1); printf("%d\n",bsearch(1,INF)); } int main() { freopen("cow.in","r",stdin); freopen("cow.out","w",stdout); init(); solve(); fclose(stdin); fclose(stdout); return 0; }View Code
相关文章推荐
- Jekyll + Github page 搭建自己免费的博客网站
- UVA - 494 Kindergarten Counting Game
- 20145325张梓靖 实验三 "敏捷开发与XP实践"
- LeetCode - Reverse String
- 2016年3月16日作业
- Stopping ADB server failed(code -1)
- Azure Redis Cache (3) 在Windows 环境下使用Redis Benchmark
- django使用html模板减少代码
- 爆打团队 2016.04.22 站立会议
- css在线参考手册
- 【Android UI】音频条形图
- Mac 命令行实例
- 第 2 章 排版样式
- 第八周作业
- HDU 1022 - Train Problem I
- 欧弈斌--人生最重要的五大投资,你选对了吗?
- Android-Handler复习(一)
- HDU-1269 迷宫城堡 (强连通分量[Kosaraju])
- Asp.net MVC 的八个扩展点
- Servlet入门笔记