zoukankan      html  css  js  c++  java
  • POJ2456(最大化最小值)

    Aggressive cows
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 10728   Accepted: 5288

    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 3
    1
    2
    8
    4
    9

    Sample Output

    3
    思路:最大化最小值、最小化最大值等问题常用二分搜索解决。
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int MAXN=100005;
    int n,m;
    int x[MAXN];
    bool test(int d)
    {
        int cnt=1;
        int last=x[0];
        for(int i=1;i<n;i++)
        {
            while(i<n&&x[i]-last<d)//两牛舍之间的距离均不小最小值d 
            {
                i++;
            }
            if(i==n)    break;
            cnt++;
            last=x[i];
        }    
        return cnt>=m;
    }
    int main()
    {
        while(cin>>n>>m)
        {
            for(int i=0;i<n;i++)
            {
                cin>>x[i];
            }
            sort(x,x+n);
            int l=0;
            int r=0x3f3f3f3f;
            while(r-l>1)
            {
                int mid=(l+r)>>1;
                if(test(mid))    l=mid;
                else r=mid;
            }        
            cout<<l<<endl;
        }
        return 0;
    }
  • 相关阅读:
    【转】ArcGIS 合并要素 Union Dissolve Append Merge
    Linux命令行下编辑常用快捷键
    rpm aid用法
    SUSE 吉祥物图片
    F9初装体验
    openSUSE10、SUSE EVAL10和SUSE10的区别[翻译]
    装有Linux DIY牛人百元人民币昂贵甜头液晶一体机
    SUSE10.3恢复GRUB体例
    64位Fedora还挑CPU
    Ubuntu8.04安置XCrysDen
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5677927.html
Copyright © 2011-2022 走看看