Corral the Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1352   Accepted: 565

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.

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.

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

Hint

Explanation of the sample:

|*   *

| * *

+------

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

|CCCC

|CCCC

|*CCC*

|C*C*

+------

Source

 
 
题意:需要你搭建一个正方形的围栏,围栏里面需要有至少C个特殊的点,问你正方形最短的边长是多少
题解:给的坐标可能会重复,所以我们按照x坐标的和y坐标从小到大排序后离散化,用二维前缀和记录区域内点的个数,然后二分答案,每次检查时检查区域内的点是否满足大于C个即可
代码如下:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ;
int C,n;
struct node{
int x,y;
}p[maxn];
int xn,yn,rx[maxn],ry[maxn];
int sum[maxn][maxn];
bool cmp1(node a,node b){
return a.x<b.x;
}
bool cmp2(node a,node b){
return a.y<b.y;
}
bool check(int k){
for(int a=,b=;;a++){
while(rx[b+]-rx[a]+<=k&&b<xn) b++;
for(int c=,d=;;c++){
while(ry[d+]-ry[c]+<=k&&d<yn) d++;
int ans=sum[b][d]+sum[a-][c-]-sum[a-][d]-sum[b][c-];
if(ans>=C) return true;
if(d==yn) break;
}
if(b==xn) break;
}
return false;
} int main(){
scanf("%d%d",&C,&n);
for(int i=;i<=n;i++){
scanf("%d%d",&p[i].x,&p[i].y);
}
sort(p+,p+n+,cmp1);
xn=;rx[]=p[].x;p[].x=;
for(int i=;i<=n;i++){
if(p[i].x!=p[i-].x) rx[++xn]=p[i].x;
p[i].x=xn;
}
sort(p+,p+n+,cmp2);
yn=;ry[]=p[].y;p[].y=;
for(int i=;i<=n;i++){
if(p[i].y!=p[i-].y) ry[++yn]=p[i].y;
p[i].y=yn;
}
for(int i=;i<=n;i++) sum[p[i].x][p[i].y]++;
for(int i=;i<=xn;i++){
for(int j=;j<=yn;j++){
sum[i][j]+=sum[i][j-]+sum[i-][j]-sum[i-][j-];
}
}
int l=,r=max(rx[xn],ry[yn]),ans;
while(l<=r){
int mid=l+r>>;
if(check(mid)){
ans=mid;
r=mid-;
}else{
l=mid+;
}
}
printf("%d\n",ans);
}

POJ 3179 Corral the Cows的更多相关文章

  1. 洛谷 P2862 [USACO06JAN]把牛Corral the Cows 解题报告

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  2. POJ 2387 Til the Cows Come Home (图论,最短路径)

    POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...

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

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

  4. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  5. 洛谷——P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  6. 洛谷P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  7. 洛谷 P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  8. 【POJ 3179】 Corral the Cows

    [题目链接] http://poj.org/problem?id=3179 [算法] 首先,我们发现答案是具有单调性的,也就是说,如果边长为C的正方形可以,那么比边长C大的正方形也可以,因此,可以二分 ...

  9. POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

随机推荐

  1. (杭电 1097)A hard puzzle

    A hard puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. win10在此处打开命令cmd

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\shell\OpenCmdHere] @="在此处打开命令 ...

  3. Python | 用Pyinstaller打包发布exe应用

    参考:https://jingyan.baidu.com/article/a378c960b47034b3282830bb.html https://ask.csdn.net/questions/72 ...

  4. 50-Identity MVC:DbContextSeed初始化

    1-创建一个可以启动时如果没有一个账号刚创建1个新的账号 namespace MvcCookieAuthSample.Data { public class ApplicationDbContextS ...

  5. WPF中的数据模板(DataTemplate)

    原文:WPF中的数据模板(DataTemplate) WPF中的数据模板(DataTemplate)                                                   ...

  6. mysqli函数库的使用

    综述 1.什么是mysqli PHP-MySQL 函数库是 PHP 操作 MySQL 资料库最原始的扩展库,PHP-MySQLi 的 i 代表 Improvement ,相当于前者的增强版,也包含了相 ...

  7. Windows下nginx作为静态资源服务器使用

    一.Nginx下载与安装 1.nginx官方下载地址:http://nginx.org/ 2.下载完后将压缩包解压即可 3.nginx配置文件为根目录下conf\nginx.conf 二.Nginx常 ...

  8. Ubuntu下使用Git_6

    这回真的是最后一篇了,哈哈,改写提交. 这里这部分在目前的学习阶段还没有用到,所以,这里将不在有实验的部分,在下面的链接中有详细的介绍 这也是我第一讲一个网站的内容完成的学习完成,这这部分,我讲简单的 ...

  9. 可以完成99%的静态页面的HTML标签

    HTML:一套浏览器认知的规则HTML分为两个部分,头和身体.一个完整的网页相当于一个裸体的人,我们利用HTML给它穿上衣服,使它更好看.下面我将为大家介绍一下HTML一些基本的标签,而这些基本的标签 ...

  10. Spark概念介绍

    Spark概念介绍:spark应用程序在集群中以一系列独立的线程运行,通过驱动器程序(Driver Program)发起一系列的并行操作.SparkContext对象作为中间的连接对象,通过Spark ...