POJ 3179 Corral the Cows
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1352 | Accepted: 565 |
Description
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
Lines 2..N+1: Each line contains two space-separated integers that are the X,Y coordinates of a clover field.
Output
Sample Input
3 4
1 2
2 1
4 1
5 2
Sample Output
4
Hint
|* *
| * *
+------
Below is one 4x4 solution (C's show most of the corral's area); many others exist.
|CCCC
|CCCC
|*CCC*
|C*C*
+------
Source
#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);
}
最新文章
- iOS-应用上架
- [转]Gridview中实现RadioButton单选效果
- 使用方便 正则表达式grep,sed,awk(一)
- BeanUtils.copyProperties VS PropertyUtils.copyProperties
- SpringBoot+gradle项目构建war
- iOS使用XZMRefresh实现UITableView或UICollectionView横向刷新
- Carbon - 在线生成精美的代码片段图片(含插件)
- MySQL数据库基本用法-聚合-分组
- 树莓派3 之 USB摄像头安装和使用
- Linux内核分析作业 NO.5
- Knockout学习之表单绑定器(下)
- Nodejs学习笔记(四)与MySQL交互(felixge/node-mysql)
- User类 新增共有属性Current ID
- 【笔试题】Java 继承知识点检测
- Android——OnCreate
- LeetCode 简单 -旋转字符串(796)
- Python3 之选课系统
- JS判断当前DOM树是否加载完毕
- What is the bitmap index?
- Mysql安装发生「Access denied for user ‘root’@’localhost’ (using password: NO)」错误