• 洛谷 P1824 进击的奶牛【二分答案/类似青蛙过河】


    题目描述
    Farmer John建造了一个有N(2<=N<=100,000)个隔间的牛棚,这些隔间分布在一条直线上,坐标是x1,...,xN (0<=xi<=1,000,000,000)。

    他的C(2<=C<=N)头牛不满于隔间的位置分布,它们为牛棚里其他的牛的存在而愤怒。为了防止牛之间的互相打斗,Farmer John想把这些牛安置在指定的隔间,所有牛中相邻两头的最近距离越大越好。那么,这个最大的最近距离是多少呢?

    输入输出格式
    输入格式:
    第1行:两个用空格隔开的数字N和C。

    第2~N+1行:每行一个整数,表示每个隔间的坐标。

    输出格式:
    输出只有一行,即相邻两头牛最大的最近距离。

    输入输出样例
    输入样例#1:
    5 3
    1
    2
    8
    4
    9

    输出样例#1:
    3

    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<assert.h>
    #include<bitset>
    #include<numeric>
    #define debug() puts("++++")
    #define gcd(a,b) __gcd(a,b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a,b,sizeof(a))
    #define sz size()
    #define be begin()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define lowbit(x) -x&x
    #define all 1,n,1
    #define rep(i,x,n) for(int i=(x); i<(n); i++)
    #define in freopen("in.in","r",stdin)
    #define out freopen("out.out","w",stdout)
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int,int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e18;
    const int maxn = 1e6 + 20;
    const int maxm = 1e6 + 10;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int dx[] = {-1,1,0,0,1,1,-1,-1};
    const int dy[] = {0,0,1,-1,1,-1,1,-1};
    int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int L,n,m;
    int a[maxn];
    bool cmp(int a,int b)
    {
        return a>b;
    }
    int check(int x)
    {
        int cnt=1,now=a[1];
        for(int i=2;i<=n;i++)
        {
            if(a[i]-now>=x)
            {
                cnt++;
                now=a[i];
            }
        }
        return cnt>=m;
    }
    
    void solve(int l,int r)
    {
        int mid,ans;
        while(l<=r)
        {
            mid = (l+r)/2;
            if(check(mid))
                l=mid+1,ans=mid;
            else r=mid-1;
        }
        printf("%d
    ",ans);
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            int L = 0, R = INF;
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&a[i]);
            }
            sort(a+1,a+n+1);//
            solve(L,R);
        }
    }
    
    
  • 相关阅读:
    计网:传输层
    计网:网络层
    codeblocks 的安装与初体验
    二叉排序树的建立
    使用颜色空间进行图像分割
    密码学笔记
    Git笔记
    SVM笔记
    GAN笔记——理论与实现
    leetcode(三)
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9363998.html
Copyright © 2020-2023  润新知