BZOJ——1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏
http://www.lydsy.com/JudgeOnline/problem.php?id=1720
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 177 Solved: 90
[Submit][Status][Discuss]
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
1 2
2 1
4 1
5 2
Sample Output
OUTPUT DETAILS:
Below is one 4x4 solution (C's show most of the corral's area); many
others exist.
|CCCC
|CCCC
|*CCC*
|C*C*
+------
HINT
Source
求最小的代价,考虑二分(logmaxlen)
发现数据范围支持n^2logmaxlen的复杂度
现将所求正方形看做是一个无限高的矩形,
O(n)枚举一个右端点,确定出左端点后,
再O(n)判断在规定的高度内能否得到C个糖果
1 #include <algorithm> 2 #include <cstdio> 3 4 inline void read(int &x) 5 { 6 x=0; register char ch=getchar(); 7 for(; ch>'9'||ch<'0'; ) ch=getchar(); 8 for(; ch>='0'&&ch<='9'; ch=getchar()) x=x*10+ch-'0'; 9 } 10 const int N(523); 11 int c,n; 12 struct Node { 13 int x,y; 14 bool operator < (const Node&a)const 15 { 16 return x<a.x; 17 } 18 }pos[N]; 19 20 int L,R,Mid,ans,cnt,tmp[N]; 21 inline bool judge(int l,int r) 22 { 23 if(r-l+1<c) return 0; cnt=0; 24 for(int i=l; i<=r; ++i) tmp[++cnt]=pos[i].y; 25 std::sort(tmp+1,tmp+cnt+1); 26 for(int i=c; i<=cnt; ++i) 27 if(tmp[i]-tmp[i-c+1]<=Mid) return 1; 28 return 0; 29 } 30 inline bool check(int x) 31 { 32 int l=1,r=1; 33 for(; r<=n; ++r) 34 { 35 if(pos[r].x-pos[l].x>x) 36 if(judge(l,r-1)) return 1; 37 for(; pos[r].x-pos[l].x>x; ) l++; 38 } 39 return judge(l,n); 40 } 41 42 int Presist() 43 { 44 read(c),read(n); 45 for(int i=1; i<=n; ++i) 46 read(pos[i].x),read(pos[i].y); 47 std::sort(pos+1,pos+1+n); 48 for(R=1e4; L<=R; ) 49 { 50 Mid=L+R>>1; 51 if(check(Mid)) 52 { 53 R=Mid-1; 54 ans=Mid+1; 55 } 56 else L=Mid+1; 57 } 58 printf("%d\n",ans); 59 return 0; 60 } 61 62 int Aptal=Presist(); 63 int main(int argc,char**argv){;}