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. 阿里云异常网络连接-可疑WebShell通信行为的分析解决办法

    2018年10月27日接到新客户网站服务器被上传了webshell脚本木马后门问题的求助,对此我们sine安全公司针对此阿里云提示的安全问题进行了详细分析,ECS服务器被阿里云提示异常网络连接-可疑W ...

  2. 集合--数据结构与算法JavaScript描述(9)

    集合 Set 概念 (跟高中时数学课上学的集合几乎一模一样哦哈哈) 集合是一种包含不同元素的数据结构. 集合中的元素称为成员. 集合的两个最重要的特性: 集合中的成员是无序的. 集合中不允许相同成员存 ...

  3. python pip ,安装,卸载,查看等命令,不同版本

    pycharm及python的使用说明   Python和 pycharm的使用 1. pycharm和Python 下载 安装后需要激活码.判断Python是否安装好了,cmd下跑: python ...

  4. kafka单机部署文档

    单机Kafka部署文档 最简单的使用方式,单机,使用自带的zookeeper 1.解压 下载地址:http://pan.baidu.com/s/1i4K2pXr tar –zxvf kafka_2.1 ...

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

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

  6. [Windows]_[C/C++]_[如何调试子进程]

    场景 1.VC++ 的程序A在启动程序C时, 如果需要调试程序C的话一般有两种, 一种是通过菜单 调试->附加到进程的方式来调试程序, 缺点就是这个进程必须先启动, 但是一启动的话有可能就执行了 ...

  7. 可用率map处理

    total_data =[ {'event_current_dealer': '陈铁', 'id__count': 66}, {'event_current_dealer': '丁凯', 'id__c ...

  8. 【连载】Maven系列(三) 进阶

    相关文章: 1.<用起来超爽的Maven——入门篇> 2.<用起来超爽的Maven——进阶篇> 一.Maven坐标: Maven世界拥有大量需要构建jar文件,我们需要找一个用 ...

  9. (1)分布式下的爬虫Scrapy应该如何做-安装

    关于Scrapy的安装,网上一搜一大把,一个一个的安装说实话是有点麻烦,那有没有一键安装的?答案显然是有的,下面就是给神器的介绍: 主页:http://conda.pydata.org/docs/ 下 ...

  10. Qt 蓝牙部分翻译

    这是我第一次尝试翻译技术文档,自己英语太烂,一直不敢尝试,感谢生活,让我勇敢迈出这第一步. 大部分都是直译,如有不妥,还请制导. Qt Bluetooth The Bluetooth API prov ...