zoukankan      html  css  js  c++  java
  • [POJ2456]Aggressive cows

     
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 8760   Accepted: 4350

    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

    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.

    Source

    THINKING

      最大化最小值的题。经分析,我们可以设C(D)=可以安排牛的位置使得任意的牛的间距都不小d,则用贪心法可以求解。首先对牛舍位置排序,把第一头牛放入x0的牛舍,如果第i头牛放入牛舍aj的话,第i+1头牛就要放入aj<=ak中,二分查找答案。

    {《挑战ACM程序设计竞赛》}
    var n,m,lb,ub,mid:int64;
        i:longint;
        a:array[-1..10000] of int64;
    
    function c(x:int64):boolean;
    var last,crt:int64;j:longint;
    begin
        last:=0;
        //crt:=0;
        for j:=1 to m-1 do
            begin
                crt:=last+1;
                while (crt<n)and(a[crt]-a[last]<x) do inc(crt);
                if crt=n then exit(false);
                last:=crt;
            end;
        exit(true);
    end;
    
    procedure sort(l,r: int64);
          var
             i,j:longint;x,y:int64;
          begin
             i:=l;
             j:=r;
             x:=a[(l+r) div 2];
             repeat
               while a[i]<x do
                inc(i);
               while x<a[j] do
                dec(j);
               if not(i>j) then
                 begin
                    y:=a[i];
                    a[i]:=a[j];
                    a[j]:=y;
                    inc(i);
                    j:=j-1;
                 end;
             until i>j;
             if l<j then
               sort(l,j);
             if i<r then
               sort(i,r);
          end;
    
    begin
        readln(n,m);
        for i:=1 to n do readln(a[i]);
        sort(1,n);
        lb:=0;
        ub:=a[n]-a[1]+1;
        while ub-lb>1 do
            begin
                mid:=(lb+ub) div 2;
                if c(mid) then lb:=mid
                else ub:=mid;
            end;
        writeln(lb-1);
    end.
    View Code
  • 相关阅读:
    [Redis-CentOS7]Redis设置连接密码(九)
    [Redis-CentOS7]Redis数据持久化(八)
    PAT Advanced 1101 Quick Sort (25分)
    PAT Advanced 1043 Is It a Binary Search Tree (25分)
    HTTP协议
    PAT Advanced 1031 Hello World for U (20分)
    自然码双拼快速记忆方案
    macOS 原生输入法设置自然码
    PAT Advanced 1086 Tree Traversals Again (25分)
    PAT Advanced 1050 String Subtraction (20分)
  • 原文地址:https://www.cnblogs.com/yangqingli/p/4886416.html
Copyright © 2011-2022 走看看