Beauty Contest

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 35459   Accepted: 10978

Description

Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates.

Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.Help Bessie by computing the maximum distance among all pairs of farms.

Input

* Line 1: A single integer, N

* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm

Output

* Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other. 

Sample Input

  1. 4
  2. 0 0
  3. 0 1
  4. 1 1
  5. 1 0

Sample Output

  1. 2

Hint

Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2) 
 
利用旋转卡壳求最远点对的距离平方。
  1. //2016.10.2
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <algorithm>
  7. #define N 50005
  8. #define eps 1e-8
  9.  
  10. using namespace std;
  11.  
  12. int n;
  13.  
  14. struct point
  15. {
  16. double x, y;
  17. point(){}
  18. point(double a, double b):x(a), y(b){}
  19. point operator-(point a){//向量减法
  20. return point(x-a.x, y-a.y);
  21. }
  22. double operator*(point a){//向量叉积
  23. return x*a.y-y*a.x;
  24. }
  25. bool operator<(const point a)const{
  26. if(fabs(x-a.x)<eps)return y<a.y;//浮点数的判等不能直接用‘==’直接比较
  27. return x<a.x;
  28. }
  29. double len2(){//向量模的平方
  30. return x*x+y*y;
  31. }
  32. }p[N];
  33.  
  34. struct polygon
  35. {
  36. int n;
  37. point p[N];
  38. }pg;
  39.  
  40. double cp(point o, point a, point b)//向量oa,ob叉积
  41. {
  42. return (a-o)*(b-o);
  43. }
  44.  
  45. void Convex(int &n)//Graham扫描法
  46. {
  47. sort(p, p+n);
  48. int top, m;
  49. pg.p[] = p[]; pg.p[] = p[]; top = ;
  50. for(int i = ; i < n; i++)//从前往后扫
  51. {
  52. while(top> && cp(p[i], pg.p[top], pg.p[top-])>=)top--;
  53. pg.p[++top] = p[i];
  54. }
  55. m = top;
  56. pg.p[++top] = p[n-];
  57. for(int i = n-; i >= ; i--)//从后往前扫
  58. {
  59. while(top>m && cp(p[i], pg.p[top], pg.p[top-])>=)top--;
  60. pg.p[++top] = p[i];
  61. }
  62. pg.n = top;
  63. }
  64.  
  65. int rotating_calipers()//旋转卡壳
  66. {
  67. int v = ;n = pg.n;
  68. double ans = ;
  69. pg.p[n] = pg.p[];
  70. for(int u = ; u < n; u++)//旋转
  71. {
  72. while(cp(pg.p[u],pg.p[u+],pg.p[v+])>cp(pg.p[u],pg.p[u+],pg.p[v]))v = (v+)%n;
  73. ans = max(ans, max((pg.p[u]-pg.p[v]).len2(), (pg.p[u+]-pg.p[v+]).len2()));
  74. }
  75. return ans;
  76. }
  77.  
  78. int main()
  79. {
  80. int n;
  81. while(scanf("%d", &n)!=EOF && n)
  82. {
  83. for(int i = ; i < n; i++)
  84. scanf("%lf%lf", &p[i].x, &p[i].y);
  85. Convex(n);
  86. int ans = rotating_calipers();
  87. printf("%d\n", ans);
  88. }
  89.  
  90. return ;
  91. }

POJ2187(旋转卡壳)的更多相关文章

  1. POJ2187 旋转卡壳 求最长直径

    给定平面上的一些散点集,求最远两点距离的平方值. 题解: 旋转卡壳求出凸包,然后根据单调性,求出最远两点的最大距离 #pragma GCC optimize(2) #pragma G++ optimi ...

  2. POJ2187 Beauty Contest (旋转卡壳算法 求直径)

    POJ2187 旋转卡壳算法如图 证明:对于直径AB 必然有某一时刻 A和B同时被卡住 所以旋转卡壳卡住的点集中必然存在直径 而卡壳过程显然是O(n)的 故可在O(n)时间内求出直径 凸包具有良好的性 ...

  3. poj2187 Beauty Contest(旋转卡壳)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Beauty Contest Time Limit: 3000MS   Memor ...

  4. [POJ2187][BZOJ1069]旋转卡壳

    旋转卡壳 到现在依然不确定要怎么读... 以最远点对问题为例,枚举凸包上的两个点是最简单的想法,时间复杂度O(n2) 我们想象用两条平行线卡着这个凸包,当其中一个向某个方向旋转的时候另一个显然也是朝同 ...

  5. [USACO2003][poj2187]Beauty Contest(凸包+旋转卡壳)

    http://poj.org/problem?id=2187 题意:老题了,求平面内最远点对(让本渣默默想到了悲剧的AHOI2012……) 分析: nlogn的凸包+旋转卡壳 附:http://www ...

  6. POJ2187 Beauty Contest(旋转卡壳)

    嘟嘟嘟 旋转卡壳模板题. 首先求出凸包. 然后\(O(n ^ 2)\)的算法很好想,但那就不叫旋转卡壳了. 考虑优化:直观的想是在枚举点的时候,对于第二层循环用二分或者三分优化,但实际上两点距离是不满 ...

  7. POJ2187(凸包+旋转卡壳)

    这道题目的大意是给出一组二维空间的顶点,计算其中距离最远的两个顶点之间的距离. 先说明凸包的概念和求法. 定义:对于多边形P,若将P中任意的两个点(包含边上)用一条线段连接,线段都落于该多边形中(含边 ...

  8. POJ-2187 Beauty Contest,旋转卡壳求解平面最远点对!

     凸包(旋转卡壳) 大概理解了凸包A了两道模板题之后在去吃饭的路上想了想什么叫旋转卡壳呢?回来无聊就搜了一下,结果发现其范围真广. 凸包: 凸包就是给定平面图上的一些点集(二维图包),然后求点集组成的 ...

  9. 算法复习——凸包加旋转卡壳(poj2187)

    题目: Description Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest ...

随机推荐

  1. PAT (Advanced Level) 1022. Digital Library (30)

    简单模拟题. 写的时候注意一些小优化,小心TLE. #include<iostream> #include<cstring> #include<cmath> #in ...

  2. Java 之final,static小结

    一.final 1.final变量: 当你在类中定义变量时,在其前面加上final关键字,那便是说,这个变量一旦被初始化便不可改变,这里不可改变的意思对基本类型来说是其值不可变,而对于对象变量来说其引 ...

  3. mysql 不同语法

    http://blog.csdn.net/kesaihao862/article/details/6718443 REPLACE INTO id_28_repayid(stub) VALUES(1); ...

  4. N皇后问题——递归求解

    比较简单,废话不说,上代码: public class NQueen { //比如:position[1]=3,表示第一行的第三列有一个皇后 private int [] position; //总的 ...

  5. Solr 按照得分score跟指定字段相乘排序

    sort=product([you_field],query($q)) desc

  6. [转]配置 VIM 的 Go 语言开发环境

    本文是针对像我这样的 VIM 小白而写的,所使用的 VIM-GO 插件虽然步骤简单但不够详细,特写此文以做记录和分享.欢迎各位大神纠正补充! 特别说明 本博文不是 Go 语言环境搭建教程,只是 VIM ...

  7. CoordinatorLayout学习笔记

    CoordinatorLayout是一个增强型的FrameLayout.它的作用有两个 作为一个布局的根布局 最为一个为子视图之间相互协调手势效果的一个协调布局 代码如下: <?xml vers ...

  8. time.setToNow() 取当前时间,月份有误

      [java] view plaincopy Time time = new Time("GMT+8"); time.setToNow(); int year = time.ye ...

  9. Mybatis学习(6)动态加载、一二级缓存

    一.动态加载: resultMap可以实现高级映射(使用association.collection实现一对一及一对多映射),association.collection具备延迟加载功能. 需求: 如 ...

  10. GitHub优秀的Android 开源项目

    GitHub上优秀Android开源项目 转载自 : http://my.eoe.cn/sisuer/archive/3348.html http://my.eoe.cn/sisuer/archive ...