POJ2187-Beauty Contest-凸包

平面最远点对

由于点数为1e5,而整数点的情况下,凸包上点的个数为sqrt(M),M为范围。

这样求出凸包之后n^2枚举维护距离就可以了

否则就用旋转卡壳。

这里用了挑战上的做法,比较简洁。

 1 #include <cstdio>
 2 #include <algorithm>
 3 #define LL long long
 4 
 5 using namespace std;
 6 
 7 const int maxn = 5e4+10;
 8 struct Point{
 9     int x,y;
10     Point(int _x=0,int _y=0):x(_x),y(_y){}
11     bool operator < (const Point &rhs) const{
12         if(x == rhs.x) return y < rhs.y;
13         else return x < rhs.x;
14     }
15     LL operator *(const Point &rhs) const{
16         return (LL)x*rhs.x+(LL)y*rhs.y;
17     }
18     LL operator ^(const Point &rhs) const{
19         return (LL)x*rhs.y - (LL)y*rhs.x;
20     }
21     Point operator -(const Point &rhs) const{
22         return Point(x-rhs.x,y-rhs.y);
23     }
24 }p[maxn],ch[maxn];
25 typedef Point Vector;
26 
27 LL dist(Point a,Point b)
28 {
29     return (a-b)*(a-b);
30 }
31 
32 int ConvexHull(Point *pt,int n)
33 {
34     sort(pt,pt+n);
35     int k = 0;
36     for(int i=0;i<n;i++)
37     {
38         while(k > 1 && ((ch[k-1]-ch[k-2])^(pt[i]-ch[k-1])) <= 0) k--;
39         ch[k++] = pt[i];
40     }
41     for(int i=n-2,t = k;i>=0;i--)
42     {
43         while(k > t && ((ch[k-1]-ch[k-2])^(pt[i]-ch[k-1])) <= 0) k--;
44         ch[k++] = pt[i];
45     }
46     return k;
47 }
48 int N;
49 int main()
50 {
51     while(~scanf("%d",&N))
52     {
53         for(int i=0;i<N;i++)
54         {
55             scanf("%d%d",&p[i].x,&p[i].y);
56         }
57         int cnt = ConvexHull(p,N);
58         LL ans = 0;
59         for(int i=0;i<cnt;i++)
60         {
61             //printf("%d %d\n",ch[i].x,ch[i].y);
62             for(int j=i+1;j<cnt;j++)
63             {
64                 ans = max(ans,dist(ch[i],ch[j]));
65             }
66         }
67         printf("%lld\n",ans);
68     }
69 }

 

posted @ 2016-07-28 00:07  Helica  阅读(129)  评论(0编辑  收藏  举报