LA4728 Squares

来源:互联网 发布:iphone是否允许网络 编辑:程序博客网 时间:2024/06/01 10:36

思路:用旋转卡壳求最远点对。

#include <iostream>#include <cmath>#include <algorithm>#include <cstdio>#include <cstring>#include <vector>#define next(i) ((i+1)%n)using namespace std;const double maxn = 401000;const double eps = 1e-8;int dcmp(double x) {    if(fabs(x) < eps) return 0;    if(x > 0) return 1;    return -1;}struct point {    double x, y;    point() {}    point(double a, double b) : x(a), y(b) {}    friend point operator - (const point &a, const point &b) {       return point(a.x-b.x, a.y-b.y);    }    friend point operator + (const point &a, const point &b) {        return point(a.x+b.x, a.y+b.y);    }};double det(const point &a, const point &b) {   return a.x * b.y - a.y * b.x;}bool comp_less(const point &a, const point &b) {    return dcmp(a.x-b.x)<0 || (dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)<0);}bool cmp(point &a, point &b) {    if(a.x==b.x && a.y==b.y) return true;    else return false;}double dist(const point &a, const point &b) {    return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);}struct polygon_convex {    vector <point> P;    polygon_convex(int Size = 0) {        P.resize(Size);    }};polygon_convex convex_hull(vector<point> a) {    polygon_convex res(2*a.size()+5);    sort(a.begin(), a.end(), comp_less);    ///a.erase(unique(a.begin(), a.end()), a.end());    a.erase(unique(a.begin(), a.end(), cmp), a.end());    int m = 0;    for(int i = 0; i < (int)a.size(); ++i) {        while(m > 1 && dcmp(det(res.P[m-1]-res.P[m-2], a[i]-res.P[m-2]))<=0)            --m;        res.P[m++] = a[i];    }    int k = m;    for(int i = int(a.size())-2; i >= 0; --i) {        while(m > k && dcmp(det(res.P[m-1]-res.P[m-2], a[i]-res.P[m-2]))<=0)            --m;        res.P[m++] = a[i];    }    res.P.resize(m);    if(a.size()>1) res.P.resize(m-1);    return res;}double convex_diameter(polygon_convex &a) {    vector<point> &p = a.P;    int n = p.size();    double maxd = 0.0;    if(n==1) {        return maxd;    }    int j = 1;    for(int i = 0; i < n; ++i) {        while(dcmp(det(p[next(i)]-p[i], p[j]-p[i])-det(p[next(i)]-p[i], p[next(j)]-p[i]))<0)        {            j = next(j);        }        double d = dist(p[i], p[j]);        if(d > maxd) maxd = d;        d = dist(p[next(i)], p[next(j)]);        if(d>maxd)  maxd = d;    }    return maxd;}int main(){    int T;    int n;    point A, B, C, D;//正方形四个点。    double x, y, w;    vector <point> v;    scanf("%d", &T);    while(T--) {        scanf("%d", &n);        v.clear();        for(int i = 0; i < n; i++) {            scanf("%lf%lf%lf", &x, &y, &w);            A = point(x, y);            B = point(x+w, y);            C = point(x, y+w);            D = point(x+w, y+w);            v.push_back(A);            v.push_back(B);            v.push_back(C);            v.push_back(D);        }        double res = 0.0;        polygon_convex result;        result = convex_hull(v);        res = convex_diameter(result);        printf("%d\n", int(res));    }    return 0;}/**input:230 0 11 0 20 0 162 1 21 4 23 2 34 4 46 5 15 1 3output:1385**/


原创粉丝点击