poj2187(Beauty Contest)

题目地址:Beauty Contest

题目大意:

    约翰的一头牛获得了“世界小姐的称号”,贝斯有了新的想法想要环球世界各地,这是要考虑的是自己的食物的问题,每个坐标都为一个地点,其中每个地点都有贝斯要的食物,所以她必须计算出各点最远距离的两个地点,好为自己的出行做准备,计算出来的最远距离平方输出。

解题思路:

    graham求出凸包各点,然后枚举凸包上的点,求出距离最大的两个点的长度。

代码:

  1 #include <algorithm>
  2 #include <iostream>
  3 #include <sstream>
  4 #include <cstdlib>
  5 #include <cstring>
  6 #include <cstdio>
  7 #include <string>
  8 #include <bitset>
  9 #include <vector>
 10 #include <queue>
 11 #include <stack>
 12 #include <cmath>
 13 #include <list>
 14 //#include <map>
 15 #include <set>
 16 using namespace std;
 17 /***************************************/
 18 #define ll long long
 19 #define int64 __int64
 20 #define PI 3.1415927
 21 /***************************************/
 22 const int INF = 0x7f7f7f7f;
 23 const double eps = 1e-8;
 24 const double PIE=acos(-1.0);
 25 const int d1x[]= {0,-1,0,1};
 26 const int d1y[]= {-1,0,1,0};
 27 const int d2x[]= {0,-1,0,1};
 28 const int d2y[]= {1,0,-1,0};
 29 const int fx[]= {-1,-1,-1,0,0,1,1,1};
 30 const int fy[]= {-1,0,1,-1,1,-1,0,1};
 31 /*vector <int>map[N];map[a].push_back(b);int len=map[v].size();*/
 32 /***************************************/
 33 void openfile()
 34 {
 35     freopen("data.in","rb",stdin);
 36     freopen("data.out","wb",stdout);
 37 }
 38 priority_queue<int> qi1;
 39 priority_queue<int, vector<int>, greater<int> >qi2;
 40 /**********************华丽丽的分割线,以上为模板部分*****************/
 41 
 42 struct node
 43 {
 44     double x,y;
 45     double angle;
 46 }p[50005];
 47 int cmp1(node a1,node a2)
 48 {
 49     if (a1.y==a2.y)
 50         return a1.x<a2.x;
 51     return a1.y<a2.y;
 52 }
 53 int cmp2(node a1,node a2)
 54 {
 55     if (a1.angle==a2.angle)
 56         return a1.x<a2.x;
 57     return a1.angle<a2.angle;
 58 }
 59 int EPS(double f)
 60 {
 61     if (fabs(f)<eps)
 62         return 0;
 63     return f>0?1:-1;
 64 }
 65 double getangle(node a1,node a2)
 66 {
 67     return atan2(a2.y-a1.y,a2.x-a1.x);
 68 }
 69 double cha(node b1,node a1)
 70 {
 71     return a1.x*b1.y-a1.y*b1.x;
 72 }
 73 double dis(node a,node b)
 74 {
 75     return (b.y-a.y)*(b.y-a.y)+(b.x-a.x)*(b.x-a.x);
 76 }
 77 int n;
 78 double r;
 79 int judge(node a,node b,node c)
 80 {
 81     int m=(a.x-b.x)*(c.y-b.y);
 82     int m1=(a.y-b.y)*(c.x-b.x);
 83     m=m-m1;
 84     if (m>0)
 85         return 1;
 86     return 0;
 87 }
 88 int main()
 89 {
 90 
 91     while(scanf("%d",&n)!=EOF)
 92     {
 93         int i,j;
 94         for(i=0;i<n;i++)
 95             scanf("%lf%lf",&p[i].x,&p[i].y);
 96         sort(p,p+n,cmp1);
 97         for(i=0;i<n;i++)
 98             p[i].angle=getangle(p[0],p[i]);
 99         sort(p,p+n,cmp2);
100         node que[50005];
101         que[0]=p[0];
102         que[1]=p[1];
103         que[2]=p[2];
104         int top=2;
105         for(i=3;i<n;i++)
106         {
107             while(top>=1&&judge(que[top-1],que[top],p[i]))
108             {
109                 top--;
110             }
111             que[++top]=p[i];
112         }
113         double sum=0;
114         for(i=0;i<top;i++)
115             for(j=i+1;j<=top;j++)
116             {
117                 int m1=dis(que[i],que[j]);
118                 if (m1>sum)
119                     sum=m1;
120             }
121         printf("%.0lf
",sum);
122     }
123     return 0;
124 }
View Code
屌丝终有逆袭日,*******。
原文地址:https://www.cnblogs.com/ZhaoPengkinghold/p/3884185.html