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个糖果

 #include <algorithm>
#include <cstdio> inline void read(int &x)
{
x=; register char ch=getchar();
for(; ch>''||ch<''; ) ch=getchar();
for(; ch>=''&&ch<=''; ch=getchar()) x=x*+ch-'';
}
const int N();
int c,n;
struct Node {
int x,y;
bool operator < (const Node&a)const
{
return x<a.x;
}
}pos[N]; int L,R,Mid,ans,cnt,tmp[N];
inline bool judge(int l,int r)
{
if(r-l+<c) return ; cnt=;
for(int i=l; i<=r; ++i) tmp[++cnt]=pos[i].y;
std::sort(tmp+,tmp+cnt+);
for(int i=c; i<=cnt; ++i)
if(tmp[i]-tmp[i-c+]<=Mid) return ;
return ;
}
inline bool check(int x)
{
int l=,r=;
for(; r<=n; ++r)
{
if(pos[r].x-pos[l].x>x)
if(judge(l,r-)) return ;
for(; pos[r].x-pos[l].x>x; ) l++;
}
return judge(l,n);
} int Presist()
{
read(c),read(n);
for(int i=; i<=n; ++i)
read(pos[i].x),read(pos[i].y);
std::sort(pos+,pos++n);
for(R=1e4; L<=R; )
{
Mid=L+R>>;
if(check(Mid))
{
R=Mid-;
ans=Mid+;
}
else L=Mid+;
}
printf("%d\n",ans);
return ;
} int Aptal=Presist();
int main(int argc,char**argv){;}

BZOJ——1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏的更多相关文章

  1. 【BZOJ1720】[Usaco2006 Jan]Corral the Cows 奶牛围栏 双指针法

    [BZOJ1720][Usaco2006 Jan]Corral the Cows 奶牛围栏 Description Farmer John wishes to build a corral for h ...

  2. BZOJ1720:[Usaco2006 Jan]Corral the Cows 奶牛围栏

    我对二分的理解:https://www.cnblogs.com/AKMer/p/9737477.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem ...

  3. bzoj1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏

    金组题什么的都要绕个弯才能AC..不想银组套模板= = 题目大意:给n个点,求最小边长使得此正方形内的点数不少于c个 首先一看题就知道要二分边长len 本来打算用二维前缀和来判断,显然时间会爆,而且坐 ...

  4. bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 -- Tarjan

    1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 Time Limit: 5 Sec  Memory Limit: 64 MB Description The N (2 & ...

  5. bzoj:1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会

    Description The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in ...

  6. bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会【tarjan】

    几乎是板子,求有几个size>1的scc 直接tarjan即可 #include<iostream> #include<cstdio> #include<cstri ...

  7. BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )

    tarjan求边双连通分量, 然后就是一棵树了, 可以各种乱搞... ----------------------------------------------------------------- ...

  8. Bzoj 1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 传递闭包,bitset

    1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 323  Solved ...

  9. 【BZOJ】1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会(tarjan)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1654 请不要被这句话误导..“ 如果两只成功跳圆舞的奶牛有绳索相连,那她们可以同属一个组合.” 这句 ...

随机推荐

  1. IjkPlayer播放器秒开优化以及常用Option设置

    https://blog.csdn.net/shareus/article/details/78585260 ijkplayer点播和直播视频 问题 解决及优化  https://blog.csdn. ...

  2. Spring根据XML配置文件 p名称空间注入属性(property后出现,简便但只针对基本数据类型管用,自定义集合等引用类型无效)

    要生成对象并通过名称空间注入属性的类 代码如下: package com.swift; public class User { private String userName; public void ...

  3. 同时使用多个UITableView

    1.xib\storyboard中给2个tableView设置constraints(等宽) 方法 : ①设置mainTableView的上\下\左\三部分的约束为0:subTableView上\下\ ...

  4. Fortran学习笔记6(函数、子程序)

    子程序Subroutine 自定义函数Function 全局变量COMMON BLOCK DATA 程序代码中,常常会在不同的地方重复用到某一功能和重复某一代码,这个时候就要使用函数.函数包括内嵌函数 ...

  5. CONTEST1001 题解

    PROBLEM A 分析 这个题属于非常基础的输出问题,一般来说见到这种题可以直接复制粘贴即可. 讲解 没有什么详细说明的直接复制粘贴即可.这样不容易出错. 代码 #include <stdio ...

  6. Watch Before You Feel Pressure

    Today's assembly is about the start of a journey. 今天的大会是一个旅程的开始. The start of the rest of your lives ...

  7. java发送email一般步骤

    java发送email一般步骤 一.引入javamail的jar包: 二.创建一个测试类,实现将要发送的邮件内容写入到计算机本地,查看是否能够将内容写入: public static void mai ...

  8. python-格式化输出(考试必考)

    Python与用户交互 如何交互 我们使用input()方法来与用户交互,但是无论我们输入的值是数字类型.字符串类型.列表类型,input的接收值都是字符串类型. name = input('请输入你 ...

  9. 自学入门 Python 优质中文资源索引

    所有资源基于 Python3 版本,全部中文内容,适用于 爬虫 / Web / 数据 方向,每个单元根据学习习惯从 书籍 / 文档 / 视频 中选择一类即可,建议任选一本书籍,然后配合文档类进行学习. ...

  10. Hive 执行查询语句报错,由于内存空间不足导致

    org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.hdfs.server.namenode.SafeModeException): Can ...