F

来源:互联网 发布:mac pptp vpn 设置 编辑:程序博客网 时间:2024/05/16 14:32
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.



大佬程序设计的太巧妙,二分竟然可以这样使用,自愧不如,附上代码一枚时常品味

#include<iostream>#include<algorithm>using namespace std;typedef long long ll;const long maxn=1e5+5;ll x[maxn];long n,c;bool f(ll m){    ll last=0,cur;    for(int i=1;i<c;i++)    {        cur=last+1;        while(cur<n&&x[cur]-x[last]<m)cur++;        if(cur==n)return false;        last=cur;    }    return true;}int main(){    cin>>n>>c;    for(long i=0;i<n;i++)        cin>>x[i];    sort(x,x+n);    ll l=x[0],r=x[n-1],mid;    while(r-l>1)    {        mid=(l+r)/2;        if(f(mid))l=mid;        else r=mid;    }    cout<<l<<endl;    return 0;}


阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 喊叫 喊麦怎么喊 喊唛 如何喊麦 喊麦教学 怎样喊麦 喊麦怎么压声 喊麦教学的基本功 怎么喊麦 宾从 宾的 啊宾 喊山红霞真实案件 喊山原著结局 喊山红霞真实案件原型 喊山结局 喊山的原型判了多久 喊山117分钟版在线 喊山的真实故事是怎样 喊山剧情 喊 山免费完整观看 新版喊山免费观看 喊山红霞原型 走山 女票拍拍拍喊痛 嗷嗷拍 燕青拳 喊泉在哪里 喊泉原理 喊泉旅游 普通喷泉被当喊泉 穷的 穷啊 沙发舞ktv喊麦词 网舞 hiphop舞 dula舞 脚步舞