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;
    }
  • 相关阅读:
    第一次作业
    Zotero引用文献格式(软件学报)
    好文索引
    【思维导图】分享以前整理的两张思维导图吧
    Nokia N9开启开发者模式
    Windows10 Ubuntu子系统折腾
    [CSAPP]并发与并行
    [读书笔记]逻辑思维,只要五步
    linux配置备忘
    从现在就开始写博客
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5677927.html
Copyright © 2011-2022 走看看