POJ 2187 求凸包上最长距离

简单的旋转卡壳题目

以每一条边作为基础,找到那个最远的对踵点,计算所有对踵点的点对距离

这里求的是距离的平方,所有过程都是int即可

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <algorithm>
 7 using namespace std;
 8 #define N 50010
 9 #define eps 1e-9
10 int n , top;
11
12 int dcmp(double x)
13 {
14     if(fabs(x)<eps) return 0;
15     return x<0?-1:1;
16 }
17
18 struct Point{
19     int x,y;
20     Point(int x=0 , int y=0):x(x),y(y){}
21     bool operator==(const Point &m) const{
22         return x==m.x&&y==m.y;
23     }
24     int squaredDis(){return x*x+y*y;}
25     void input(){scanf("%d%d" , &x , &y);}
26     void print(){cout<<x<<" "<<y<<endl;}
27 }po[N] , rec[N] , p;
28 typedef Point Vector;
29
30 Vector operator+(Vector a , Vector b){return Vector(a.x+b.x , a.y+b.y);}
31 Vector operator-(Vector a , Vector b){return Vector(a.x-b.x , a.y-b.y);}
32 Vector operator*(Vector a , double b){return Vector(a.x*b , a.y*b);}
33 Vector operator/(Vector a , double b){return Vector(a.x/b , a.y/b);}
34
35 int Cross(Vector a , Vector b){return a.x*b.y-b.x*a.y;}
36 double Len(Vector a){return sqrt(a.x*a.x*1.0+a.y*a.y);}
37
38 bool cmp(Point a , Point b){
39     int v = Cross(a , b);
40     if(v == 0) return a.x<b.x;
41     else return v>0;
42 }
43
44 void Graham(Point *a , Point *rec)
45 {
46     sort(a , a+n , cmp);
47   //  for(int i=0 ; i<n ; i++) cout<<i<<" "<<a[i].x<< " "<<a[i].y<<endl;
48     rec[0] = a[0] , rec[1] = a[1];
49     top=1;
50     for(int i=2 ; i<n ; i++){
51         while(top>0 && Cross(rec[top]-rec[top-1] , a[i]-rec[top-1])<=0)
52             top--;
53         rec[++top] = a[i];
54     }
55    // for(int i=0 ; i<=top ; i++) cout<<i<<" "<<rec[i].x<< " "<<rec[i].y<<endl;
56     int tmp = top;
57     for(int i=n-1 ; i>=0 ; i--){
58         while(top>tmp && Cross(rec[top]-rec[top-1] , a[i]-rec[top-1])<=0)
59             top--;
60         rec[++top]=a[i];
61     }
62   //  for(int i=0 ; i<=top ; i++) cout<<i<<" "<<rec[i].x<< " "<<rec[i].y<<endl;
63 }
64
65 int maxDis(Point *a)
66 {
67     int la=top-1 , p=0 , q=0;
68     int maxn = 0;
69     for(p=0 ; p<top ; p++){
70         while(Cross(a[p]-a[la] , a[q+1]-a[la]) - Cross(a[p]-a[la] , a[q]-a[la])>0)
71             q=(q+1)%top;
72         maxn = max(maxn , (a[q]-a[la]).squaredDis());
73         maxn = max(maxn , (a[q]-a[p]).squaredDis());
74         la = p;
75     }
76     return maxn;
77 }
78
79 int main()
80 {
81    // freopen("a.in" , "r" , stdin);
82     while(~scanf("%d" , &n))
83     {
84         for(int i=0 ; i<n ; i++) po[i].input();
85         p = po[0];
86         for(int i=1 ;i<n ; i++)
87             if(po[i].y<p.y||(po[i].y==p.y&&po[i].x<p.x)) p=po[i];
88         Graham(po , rec);
89         int ans = maxDis(rec);
90         cout<<ans<<endl;
91     }
92     return 0;
93 }
时间: 2024-11-13 09:17:32

POJ 2187 求凸包上最长距离的相关文章

【POJ 2187】 Beauty Contest (凸包-Graham扫描算法)

[POJ 2187] Beauty Contest (凸包-Graham扫描算法) 找平面最远点对 数据很大 用暴力会T..我感觉-- 扫描出个凸包 然后枚举凸包上的点即可 没坑 int也可过 注意重边跟共线就行 代码下附赠几组数据 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <vector> #include

hdu 3934&amp;&amp;poj 2079 (凸包+旋转卡壳+求最大三角形面积)

链接:http://poj.org/problem?id=2079 Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 8173   Accepted: 2423 Description Given n distinct points on a plane, your task is to find the triangle that have the maximum area, whose vertices

POJ 3348 Cows 凸包 求面积

LINK 题意:给出点集,求凸包的面积 思路:主要是求面积的考察,固定一个点顺序枚举两个点叉积求三角形面积和除2即可 /** @Date : 2017-07-19 16:07:11 * @FileName: POJ 3348 凸包面积 叉积.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : $Id$ */ #include <std

Wall - POJ 1113(求凸包)

题目大意:给N个点,然后要修建一个围墙把所有的点都包裹起来,但是要求围墙距离所有的点的最小距离是L,求出来围墙的长度. 分析:如果没有最小距离这个条件那么很容易看出来是一个凸包,然后在加上一个最小距离L,那么就是在凸包外延伸长度为L,如下图,很明显可以看出来多出来的长度就是半径为L的圆的周长,所以总长度就是凸包的周长+半径为L的圆的周长. 代码如下: -------------------------------------------------------------------------

POJ 1113 Wall 卷包裹法求凸包

Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31199   Accepted: 10521 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he w

POJ 1113 Wall(Graham求凸包周长)

题目链接 题意 : 求凸包周长+一个完整的圆周长. 因为走一圈,经过拐点时,所形成的扇形的内角和是360度,故一个完整的圆. 思路 : 求出凸包来,然后加上圆的周长 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <cmath> 5 #include <algorithm> 6 7 const double PI = acos(-1.0) ;

poj 3348 Cows 求凸包面积

题目链接 大意: 求凸包的面积. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set> #include <string> #include <queue>

POJ 3528 求三维凸包表面积

也是用模板直接套的题目诶 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <cstring> #include <cmath> #include <stack> #include <queue> #include <vector>

poj 2187 最远点对

题意: 给出n个点,求最远点对的距离. 限制: 2 <= n <= 5*1e4 思路: 凸包,旋转卡壳 /*poj 2187 题意: 给出n个点,求最远点对的距离. 限制: 2 <= n <= 5*1e4 思路: 凸包,旋转卡壳 */ #include<iostream> #include<cstdio> #include<vector> #include<algorithm> #include<cmath> using