poj 2187 Beauty Contest
生活随笔
收集整理的這篇文章主要介紹了
poj 2187 Beauty Contest
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
http://poj.org/problem?id=2187??
題意:給你n個(gè)坐標(biāo)點(diǎn) ,求兩點(diǎn)之間的最大距離的平方 題解:求出凸包,枚舉頂點(diǎn)兩兩之間的距離 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<cmath> using namespace std; #define eps 1e-8 int top , n; //點(diǎn) struct POINT {double x, y;POINT(){ }POINT(double a, double b){x = a;y = b;} }p[50005],st[50005]; //線(xiàn)段 struct Seg {POINT a, b;Seg() { }Seg(POINT x, POINT y){a = x;b = y;} }; //叉乘 double cross(POINT o, POINT a, POINT b) {return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y); }//多邊形面積,需要有順序,順(逆)時(shí)針。double area() {double ans = 0;for(int i = 1; i < top; i ++){ans += cross(p[0], p[i], p[i + 1]);}return ans; } //找凸包基點(diǎn)排序 bool cmp0(POINT a, POINT b) {if(a.y < b.y) return true;else if(a.y == b.y && a.x < b.x) return true;return false; } double dis(POINT a,POINT b){return sqrt( (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) ); } //極角排序 bool cmp1(POINT a, POINT b) {if(cross(p[0], a, b) > eps) return true;else if(fabs(cross(p[0], a, b)) < eps && dis(p[0], a) - dis(p[0], b) > eps) return true;return false; } //Graham_scan 求凸包.所求為純凈凸包... void Graham_scan() {sort(p, p + n, cmp0);sort(p + 1, p + n, cmp1);top = 0;p[n] = p[0];st[top ++] = p[0]; st[top ++] = p[1];for(int i = 2; i <= n; i ++){while(top > 2 && (cross(st[top - 1], st[top - 2], p[i]) > eps || fabs(cross(st[top - 1], st[top - 2], p[i])) < eps)) top --;st[top ++] = p[i];}top --; } double dis2(POINT a , POINT b){return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); } int main(){while(~scanf("%d",&n)){for(int i = 0;i < n;i++)scanf("%lf %lf",&p[i].x,&p[i].y);if(n == 2) {printf("%.lf\n",dis2(p[0],p[1]));continue;}Graham_scan();double _max = 0;for(int i = 0;i < top;i++)for(int j = i+1;j < top;j++)_max = max(_max,dis2(st[i],st[j]));printf("%.0lf\n",_max);} }總結(jié)
以上是生活随笔為你收集整理的poj 2187 Beauty Contest的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: hdu-You can Solve a
- 下一篇: hdu-2204 Eddy's爱好 n