博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2456 Aggressive cows 整数二分写法 模板题
阅读量:4112 次
发布时间:2019-05-25

本文共 1784 字,大约阅读时间需要 5 分钟。

Aggressive cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9129 Accepted: 4547

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 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.

Source

分析:注意分析区间的开闭性
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std; #define MM(a) memset(a,0,sizeof(a)) typedef long long LL; typedef unsigned long long ULL; const int mod = 1000000007; const double eps = 1e-10; const int inf = 0x3f3f3f3f; int x[100005]; int n,m,maxn; int ok(int d) { int p=x[1],cnt=1; for(int i=2;i<=n;i++) if(x[i]-p>=d) { p=x[i]; cnt++; if(cnt>=m) return 1; } return 0; } int main() { while(~scanf("%d %d",&n,&m)) { maxn=0; for(int i=1;i<=n;i++) { scanf("%d",&x[i]); if(x[i]>maxn) maxn=x[i]; } sort(x+1,x+n+1); int l=0,r=maxn+1,mid; while(r-l>1) { mid=(l+r)/2; if(ok(mid)) l=mid; else r=mid; } printf("%d\n",l); //左闭右开区间,输出l; } return 0; }

转载地址:http://kvgsi.baihongyu.com/

你可能感兴趣的文章
宝宝正常体温及发热处理
查看>>
c#.net常用函数列表
查看>>
怎么对待脾气暴躁爱骂人的女人?
查看>>
宝宝身高体重对照表
查看>>
面对火灾怎么办?
查看>>
宝宝周岁抓周需要准备的东西
查看>>
让婴儿早一天活动起来
查看>>
petshop4.0数据库分析一:数据库概览
查看>>
宝贝度夏从"衣食住行"着手
查看>>
一周岁宝宝的多钙食谱
查看>>
孩子发烧,别急着降温
查看>>
VS.net 安装、调试的常见问题与错误
查看>>
VS.net 安装、调试的常见问题与错误
查看>>
Visual Studio不能启动ASP.NET或ATL SERVER调试
查看>>
清除sqlserver数据库日志
查看>>
urlscan使用详解
查看>>
10款辅食做法,解决宝宝不爱吃蔬菜的难题
查看>>
SQL Server 2005和SQL Server 2000数据的相互导入
查看>>
可执行文件不能运行的解决方法
查看>>
打开窗口后为什么任务栏上没有显示
查看>>