洛谷 P2862 [USACO06JAN]把牛Corral the Cows
题目描述
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< 500)个草场,来供应她们的午餐.
约翰的土地上共有C<=N<=500)个草场,每个草场在一块1x1的方格内,而且这个方格的 坐标不会超过10000.有时候,会有多个草场在同一个方格内,那他们的坐标就会相同.
告诉约翰,最小的围栏的边长是多少?
输入输出格式
输入格式:
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.
输出格式:
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.
输入输出样例
说明
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*
+------
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int c,n,l,r,mid; struct nond{ int x,y; }v[501]; bool judge(){ int bns=0,cns=0,dns=0,ens=0; for(int i=1;i<=n;i++){ int tx1=v[i].x,dx1=v[i].x+mid-1; int ty1=v[i].y,dy1=v[i].y+mid-1; int tx2=v[i].x-mid+1,dx2=v[i].x; int ty2=v[i].y-mid+1,dy2=v[i].y; int tx3=v[i].x,dx3=v[i].x+mid-1; int ty3=v[i].y-mid+1,dy3=v[i].y; int tx4=v[i].x-mid+1,dx4=v[i].x; int ty4=v[i].y,dy4=v[i].y+mid-1; for(int j=1;j<=n;j++){ if(v[j].x>=tx1&&v[j].x<=dx1&&v[j].y>=ty1&&v[j].y<=dy1) bns++; if(v[j].x>=tx2&&v[j].x<=dx2&&v[j].y>=ty2&&v[j].y<=dy2) cns++; if(v[j].x>=tx3&&v[j].x<=dx3&&v[j].y>=ty3&&v[j].y<=dy3) dns++; if(v[j].x>=tx4&&v[j].x<=dx4&&v[j].y>=ty4&&v[j].y<=dy4) ens++; } if(bns>=c||cns>=c||dns>=c||ens>=c) return true; bns=0;cns=0;dns=0;ens=0; } return false; } int main(){ //freopen("testdata.in","r",stdin); scanf("%d%d",&c,&n); for(int i=1;i<=n;i++) scanf("%d%d",&v[i].x,&v[i].y); l=0;r=10010; while(l<=r){ mid=(l+r)/2; if(judge()) r=mid-1; else l=mid+1; } cout<<l; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
https://www.luogu.org/problemnew/solution/P2862