【BZOJ1720】[Usaco2006 Jan]Corral the Cows 奶牛围栏

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.

    约翰打算建一个围栏来圈养他的奶牛.作为最挑剔的兽类,奶牛们要求这个围栏必须是正方形的,而且围栏里至少要有C(1≤C≤500)个草场,来供应她们的午餐.
    约翰的土地上共有N(C≤N≤500)个草场,每个草场在一块lxl的方格内,而且这个方格的坐标不会超过10000.有时候,会有多个草场在同一个方格内,那他们的坐标就会相同.
    告诉约翰,最小的围栏的边长是多少?

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.

    第1行输入C和N,接下来N行每行输入一对整数,表示一个草场所在方格的坐标

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
OUTPUT DETAILS:
Below is one 4x4 solution (C's show most of the corral's area); many
others exist.

|CCCC
|CCCC
|*CCC*
|C*C*
+------

题解:二维双指针法的完美结合

双指针法就是令l=1,从1到n枚举右指针r,然后始终保证[l,r]的区间是满足题目要求的区间,不满足就使l++,并每次用[l,r]更新答案(感觉就是简化的单调队列)

如果坐标都是一维的,我们只用双指针法就能搞定,但是由于是二维的,所以我们要用两重双指针法(四指针法。。。)

先二分答案,分别用双指针法维护纵坐标和横坐标,具体还是看代码吧~

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5. using namespace std;
  6. int n,m,nx,ny;
  7. struct field
  8. {
  9. int x,y;
  10. }p[510];
  11. int rx[510],ry[510],s[510];
  12. bool cmp1(field a,field b)
  13. {
  14. return a.x<b.x;
  15. }
  16. bool cmp2(field a,field b)
  17. {
  18. return a.y<b.y;
  19. }
  20. bool solve(int ml)
  21. {
  22. int i,a,b,c,d,sc,sd;
  23. a=b=0;
  24. memset(s,0,sizeof(s));
  25. while(b<n&&rx[p[b+1].x]-rx[1]+1<=ml) s[p[++b].y]++;
  26. for(;b<=n;s[p[++b].y]++)
  27. {
  28. while(rx[p[b].x]-rx[p[a+1].x]+1>ml) s[p[++a].y]--;
  29. c=d=sc=sd=0;
  30. while(d<ny&&ry[d+1]-ry[1]+1<=ml) sd+=s[++d];
  31. for(;d<=ny;sd+=s[++d])
  32. {
  33. while(ry[d]-ry[c+1]+1>ml) sc+=s[++c];
  34. if(sd-sc>=m) return true;
  35. }
  36. }
  37. return false;
  38. }
  39. int main()
  40. {
  41. scanf("%d%d",&m,&n);
  42. int i;
  43. rx[0]=ry[0]=-1;
  44. for(i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y);
  45. sort(p+1,p+n+1,cmp2);
  46. for(i=1;i<=n;i++)
  47. {
  48. if(p[i].y>ry[ny]) ry[++ny]=p[i].y;
  49. p[i].y=ny;
  50. }
  51. sort(p+1,p+n+1,cmp1);
  52. for(i=1;i<=n;i++)
  53. {
  54. if(p[i].x>rx[nx]) rx[++nx]=p[i].x;
  55. p[i].x=nx;
  56. }
  57. int l=1,r=max(rx[nx],ry[ny]),mid;
  58. while(l<r)
  59. {
  60. mid=l+r>>1;
  61. if(solve(mid)) r=mid;
  62. else l=mid+1;
  63. }
  64. printf("%d",r);
  65. return 0;
  66. }

【BZOJ1720】[Usaco2006 Jan]Corral the Cows 奶牛围栏 双指针法的更多相关文章

  1. bzoj1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏

    金组题什么的都要绕个弯才能AC..不想银组套模板= = 题目大意:给n个点,求最小边长使得此正方形内的点数不少于c个 首先一看题就知道要二分边长len 本来打算用二维前缀和来判断,显然时间会爆,而且坐 ...

  2. BZOJ1720:[Usaco2006 Jan]Corral the Cows 奶牛围栏

    我对二分的理解:https://www.cnblogs.com/AKMer/p/9737477.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem ...

  3. BZOJ——1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏

    http://www.lydsy.com/JudgeOnline/problem.php?id=1720 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1 ...

  4. bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 -- Tarjan

    1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 Time Limit: 5 Sec  Memory Limit: 64 MB Description The N (2 & ...

  5. 【BZOJ1654】[Usaco2006 Jan]The Cow Prom 奶牛舞会 赤果果的tarjan

    Description The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in ...

  6. bzoj1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会

    Description The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in ...

  7. bzoj:1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会

    Description The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in ...

  8. 【BZOJ】1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会(tarjan)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1654 请不要被这句话误导..“ 如果两只成功跳圆舞的奶牛有绳索相连,那她们可以同属一个组合.” 这句 ...

  9. 【强连通分量】Bzoj1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会

    Description 约翰的N(2≤N≤10000)只奶牛非常兴奋,因为这是舞会之夜!她们穿上礼服和新鞋子,别上鲜花,她们要表演圆舞.     只有奶牛才能表演这种圆舞.圆舞需要一些绳索和一个圆形的 ...

随机推荐

  1. java判断邮件是否发送成功

    http://www.cnblogs.com/winner-0715/p/5136392.html

  2. 免费 web api 接口大全

    下面的接口来自互联网,部分功能需要付费 查询手机 http://www.yodao.com/s-martresult-xml/search.s?type=mobile&q= 手机号码 查询 I ...

  3. 关于Cocos2d-x中数据的存储

    当局分数的打印和最高分数的记录 1.首先定义一个Label类型的节点在GameScene.cpp的init方法中,设置初始分数为0 _myScore = 0; scorelabel = Label:: ...

  4. Android代码的几点小技巧

     1)View的状态保存与恢复dispatchRestoreInstanceStateonRestoreInstanceStateonSaveInstanceState 2)Service的前台服务使 ...

  5. python中时间操作总结

    一.time 二.datetime 1.获取当前系统时间 datenow = datetime.datetime.now() 2.将datetime格式的时间转换成str datenow = date ...

  6. The configuration file 'appsettings.json' was not found and is not optional

    问: .Net Core: Application startup exception: System.IO.FileNotFoundException: The configuration file ...

  7. 開始学习swift,资料汇总帖

    最近開始学习swift,以后mac和ios开发就指望它,曾经学oc半途而废了.主要原因是oc等语法实在能适应,如今有swift了.语法有js,scala,python,c++,oc等语言的影子,又一次 ...

  8. u3d调用c++ dll的DllNotFoundExceion 问题

    原文地址:http://blog.csdn.net/boren31/article/details/8778504 问题年年有,今年特别多. 开发环境: Windows  XP sp3 Visual  ...

  9. (转)fiddler模拟post请求

    转自:https://www.cnblogs.com/xiaoxi-3-/p/7612254.html 前言: Fiddler是一个简单的http协议调试代理工具,它界面友好,易于操作,是模拟http ...

  10. iOS获取时间、日期

    //获取当前时间 NSDateFormatter *formatter = [[[NSDateFormatter alloc] init]autorelease]; [formatter setLoc ...