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);
}

最新文章

  1. iOS-应用上架
  2. [转]Gridview中实现RadioButton单选效果
  3. 使用方便 正则表达式grep,sed,awk(一)
  4. BeanUtils.copyProperties VS PropertyUtils.copyProperties
  5. SpringBoot+gradle项目构建war
  6. iOS使用XZMRefresh实现UITableView或UICollectionView横向刷新
  7. Carbon - 在线生成精美的代码片段图片(含插件)
  8. MySQL数据库基本用法-聚合-分组
  9. 树莓派3 之 USB摄像头安装和使用
  10. Linux内核分析作业 NO.5
  11. Knockout学习之表单绑定器(下)
  12. Nodejs学习笔记(四)与MySQL交互(felixge/node-mysql)
  13. User类 新增共有属性Current ID
  14. 【笔试题】Java 继承知识点检测
  15. Android——OnCreate
  16. LeetCode 简单 -旋转字符串(796)
  17. Python3 之选课系统
  18. JS判断当前DOM树是否加载完毕
  19. What is the bitmap index?
  20. Mysql安装发生「Access denied for user ‘root’@’localhost’ (using password: NO)」错误

热门文章

  1. UVA - 12230
  2. 怎么实现hibernate悲观锁和乐观锁?
  3. 集成activiti到现有项目中
  4. P1215 [USACO1.4]母亲的牛奶 Mother&#39;s Milk
  5. L009文件属性知识详解小节
  6. spring 读取properties文件--通过注解方式
  7. c# 3D图形处理库
  8. 51单片机实现定时器中断0-F
  9. MySQL训练营03
  10. 今日头条 2018 AI Camp 6 月 2 日在线笔试编程题第二道——两数差的和