博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2456 Aggressive cows (二分)
阅读量:6292 次
发布时间:2019-06-22

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

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.

题意:

FJ搭建了一间有N间牛舍的小屋。牛舍在一排直线上,第i号牛舍在xi的位置上。每头牛尽量放在尽可能远的牛舍,求最大化最近的两头牛之间的距离。

题解:

二分搞一下就好了。

#include
#include
using namespace std;const int INF=1e9+5,maxn=1e5+5;int n,k;int a[maxn];bool check(int x){ int cur=0,cnt=1; for(int i=1;i
=x) cur=i,cnt++; if(cnt>=k) break; } return cnt>=k;}void solve(){ int lb=0,ub=INF; while(lb<=ub) { int mid=(lb+ub)>>1; if(check(mid)) lb=mid+1; else ub=mid-1; } cout<
<
>n>>k; for(int i=0;i
>a[i]; sort(a,a+n); solve(); return 0;}

转载于:https://www.cnblogs.com/orion7/p/7682653.html

你可能感兴趣的文章
写给0-3岁产品经理的12封信(第08篇)——产品运营能力
查看>>
ArcGIS Engine 符号自动化配置工具实现
查看>>
小程序 · 跳转带参数写法,兼容url的出错
查看>>
flutter error
查看>>
Flask框架从入门到精通之模型数据库配置(十一)
查看>>
10年重新出发
查看>>
2019年-年终总结
查看>>
聊聊elasticsearch的RoutingService
查看>>
让人抓头的Java并发(一) 轻松认识多线程
查看>>
从源码剖析useState的执行过程
查看>>
地包天如何矫正?
查看>>
中间件
查看>>
Android SharedPreferences
查看>>
css面试题
查看>>
Vue组建通信
查看>>
用CSS画一个带阴影的三角形
查看>>
前端Vue:函数式组件
查看>>
程鑫峰:1.26特朗.普力挺美元力挽狂澜,伦敦金行情分析
查看>>
safari下video标签无法播放视频的问题
查看>>
01 iOS中UISearchBar 如何更改背景颜色,如何去掉两条黑线
查看>>