• BZOJ——1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏


    http://www.lydsy.com/JudgeOnline/problem.php?id=1720

    Time Limit: 5 Sec  Memory Limit: 64 MB
    Submit: 177  Solved: 90
    [Submit][Status][Discuss]

    Description

    Farmer John wishes to build a corral for his cows. Being finicky beasts, they demand that the corral be square and that the corral contain at least C (1 <= C <= 500) clover fields for afternoon treats. The corral's edges must be parallel to the X,Y axes. FJ's land contains a total of N (C <= N <= 500) clover fields, each a block of size 1 x 1 and located at with its lower left corner at integer X and Y coordinates each in the range 1..10,000. Sometimes more than one clover field grows at the same location; such a field would have its location appear twice (or more) in the input. A corral surrounds a clover field if the field is entirely located inside the corral's borders. Help FJ by telling him the side length of the smallest square containing C clover fields.

        约翰打算建一个围栏来圈养他的奶牛.作为最挑剔的兽类,奶牛们要求这个围栏必须是正方形的,而且围栏里至少要有C(1≤C≤500)个草场,来供应她们的午餐.
        约翰的土地上共有N(C≤N≤500)个草场,每个草场在一块lxl的方格内,而且这个方格的坐标不会超过10000.有时候,会有多个草场在同一个方格内,那他们的坐标就会相同.
        告诉约翰,最小的围栏的边长是多少?
     

    Input

    * Line 1: Two space-separated integers: C and N

    * Lines 2..N+1: Each line contains two space-separated integers that are the X,Y coordinates of a clover field.

        第1行输入C和N,接下来N行每行输入一对整数,表示一个草场所在方格的坐标

    Output

    * Line 1: A single line with a single integer that is length of one edge of the minimum size square that contains at least C clover fields.

        输入最小边长.

    Sample Input

    3 4
    1 2
    2 1
    4 1
    5 2

    Sample Output

    4

    OUTPUT DETAILS:

    Below is one 4x4 solution (C's show most of the corral's area); many
    others exist.

    |CCCC
    |CCCC
    |*CCC*
    |C*C*
    +------

    HINT

     

    Source

    Gold

    求最小的代价,考虑二分(logmaxlen)
    发现数据范围支持n^2logmaxlen的复杂度
    现将所求正方形看做是一个无限高的矩形,
    O(n)枚举一个右端点,确定出左端点后,
    再O(n)判断在规定的高度内能否得到C个糖果

     1 #include <algorithm>
     2 #include <cstdio>
     3 
     4 inline void read(int &x)
     5 {
     6     x=0; register char ch=getchar();
     7     for(; ch>'9'||ch<'0'; ) ch=getchar();
     8     for(; ch>='0'&&ch<='9'; ch=getchar()) x=x*10+ch-'0';
     9 }
    10 const int N(523);
    11 int c,n;
    12 struct Node {
    13     int x,y;
    14     bool operator < (const Node&a)const
    15     {
    16         return x<a.x;
    17     }
    18 }pos[N];
    19 
    20 int L,R,Mid,ans,cnt,tmp[N];
    21 inline bool judge(int l,int r)
    22 {
    23     if(r-l+1<c) return 0; cnt=0;
    24     for(int i=l; i<=r; ++i) tmp[++cnt]=pos[i].y;
    25     std::sort(tmp+1,tmp+cnt+1);
    26     for(int i=c; i<=cnt; ++i)
    27         if(tmp[i]-tmp[i-c+1]<=Mid) return 1;
    28     return 0;
    29 }
    30 inline bool check(int x)
    31 {
    32     int l=1,r=1;
    33     for(; r<=n; ++r)
    34     {
    35         if(pos[r].x-pos[l].x>x)
    36             if(judge(l,r-1)) return 1;
    37         for(; pos[r].x-pos[l].x>x; ) l++;
    38     }
    39     return judge(l,n);
    40 }
    41 
    42 int Presist()
    43 {
    44     read(c),read(n);
    45     for(int i=1; i<=n; ++i)
    46         read(pos[i].x),read(pos[i].y);
    47     std::sort(pos+1,pos+1+n);
    48     for(R=1e4; L<=R; )
    49     {
    50         Mid=L+R>>1;
    51         if(check(Mid))
    52         {
    53             R=Mid-1;
    54             ans=Mid+1;
    55         }
    56         else L=Mid+1;
    57     }
    58     printf("%d
    ",ans);
    59     return 0;
    60 }
    61 
    62 int Aptal=Presist();
    63 int main(int argc,char**argv){;}
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    [LintCode 614.] 二叉树的最长连续子序列 II
    [LintCode 90.] k数和 II
    [LintCode 1674.] 倒可乐
    [LintCode 797.] 到达一个数字
    [LintCode 1691.] 买卖股票的最佳时机V
    [LintCode 69. 242.] 二叉树的层次遍历
    [LintCode 229.] 栈排序
    [LeetCode 1671.] 玩游戏
    [LintCode 1668.] 区间最小覆盖
    (十)线程同步
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7731846.html
Copyright © 2020-2023  润新知