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;
    }
  • 相关阅读:
    unicode下各种类型转换CString、string
    单元测试简介
    linux系统目录详解
    Python 几个重要的内置函数
    python join split
    将博客搬至CSDN
    COCOS2DX3.0的3种触摸响应机制
    [转] 千万别在最能吃苦的年纪选择安逸
    明白什么是“五险一金”
    MAC 安装更新 ANT
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5677927.html
Copyright © 2011-2022 走看看