poj 2456 Aggressive cows

来源:互联网 发布:mac 图标不放大 编辑:程序博客网 时间:2024/06/06 13:20

题目链接:点这里。

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.

【题意】

农场主要养动物,修了很多厩,告诉你这些厩的位置,让你放给定数量的动物进去,使得相邻两个动物之间的距离最小值最大化。

【分析】

这种题做多了就没啥意思了,显然的二分答案。然后依次判断当前答案是否是正确的。然后就没啥了。

【代码】

#include<iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>#include<stack>#include<set>#include<map>#include<queue>#include<sstream>using namespace std;#define rep0(i,l,r) for(int i = (l);i < (r);i++)#define rep1(i,l,r) for(int i = (l);i <= (r);i++)#define rep_0(i,r,l) for(int i = (r);i > (l);i--)#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)#define MS0(a) memset(a,0,sizeof(a))#define MS_1(a) memset(a,-1,sizeof(a))#define MSinf(a) memset(a,0x3f,sizeof(a))#define MSfalse(a) memset(a,false,sizeof(a))#define pin1(a) printf("%d",(a))#define pin2(a,b) printf("%d %d",(a),(b))#define pin3(a,b,c) printf("%d %d %d",(a),(b),(c))#define pll1(a) printf("%lld",(a))#define pll2(a,b) printf("%lld %lld",(a),(b))#define pll3(a,b,c) printf("%lld %lld %lld",(a),(b),(c))#define pdo1(a) printf("%f",(a))#define pdo2(a,b) printf("%f %f",(a),(b))#define pdo3(a,b,c) printf("%f %f %f",(a),(b),(c))#define huiche puts("")#define inf 0x3f3f3f3f#define lson (ind<<1),l,mid#define rson ((ind<<1)|1),mid+1,r#define uint unsigned inttypedef pair<int,int> PII;#define A first#define B second#define pb push_back#define mp make_pair#define ll long long#define eps 1e-8#define pi acos(-1.0)inline void read1(int &num) {    char in;    bool IsN=false;    in=getchar();    while(in!='-'&&(in<'0'||in>'9')) in=getchar();    if(in=='-') {        IsN=true;        num=0;    } else num=in-'0';    while(in=getchar(),in>='0'&&in<='9') {        num*=10,num+=in-'0';    }    if(IsN) num=-num;}inline void read2(int &a,int &b) {    read1(a);    read1(b);}inline void read3(int &a,int &b,int &c) {    read1(a);    read1(b);    read1(c);}inline void read1(ll &num) {    char in;    bool IsN=false;    in=getchar();    while(in!='-'&&(in<'0'||in>'9')) in=getchar();    if(in=='-') {        IsN=true;        num=0;    } else num=in-'0';    while(in=getchar(),in>='0'&&in<='9') {        num*=10,num+=in-'0';    }    if(IsN) num=-num;}inline void read2(ll &a,ll &b) {    read1(a);    read1(b);}inline void read3(ll &a,ll &b,ll &c) {    read1(a);    read1(b);    read1(c);}inline void read1(double &num) {    char in;    double Dec=0.1;    bool IsN=false,IsD=false;    in=getchar();    while(in!='-'&&in!='.'&&(in<'0'||in>'9'))        in=getchar();    if(in=='-') {        IsN=true;        num=0;    } else if(in=='.') {        IsD=true;        num=0;    } else num=in-'0';    if(!IsD) {        while(in=getchar(),in>='0'&&in<='9') {            num*=10;            num+=in-'0';        }    }    if(in!='.') {        if(IsN) num=-num;        return ;    } else {        while(in=getchar(),in>='0'&&in<='9') {            num+=Dec*(in-'0');            Dec*=0.1;        }    }    if(IsN) num=-num;}inline void read2(double &a,double &b) {    read1(a);    read1(b);}inline void read3(double &a,double &b,double &c) {    read1(a);    read1(b);    read1(c);}//--------------------------------------------------------------------------------------------------------------int m,n;const int maxm = 1e5+10;int num[maxm];bool pan(int ind){    int x = 1;    int len = 0;    rep0(i,1,m)    {        len+=num[i]-num[i-1];        if(len>=ind)        {            len=0;            x++;        }    }    if(x>=n) return true;    return false;}int main() {//    freopen("in.txt","r",stdin);    while(scanf("%d%d",&m,&n)!=EOF)    {        rep0(i,0,m) read1(num[i]);        sort(num,num+m);        int l = 0,r = num[m-1]-num[0];        while(l+5<r)        {            int mid = (l+r)>>1;            if(pan(mid)) l = mid;            else r = mid-1;        }        rep_1(i,r,l)        if(pan(i))         {            printf("%d\n",i);            break;        }    }    return 0;}
原创粉丝点击