zoukankan      html  css  js  c++  java
  • POJ 2079 Triangle [旋转卡壳]

    Triangle
    Time Limit: 3000MS   Memory Limit: 30000K
    Total Submissions: 9525   Accepted: 2845

    Description

    Given n distinct points on a plane, your task is to find the triangle that have the maximum area, whose vertices are from the given points.

    Input

    The input consists of several test cases. The first line of each test case contains an integer n, indicating the number of points on the plane. Each of the following n lines contains two integer xi and yi, indicating the ith points. The last line of the input is an integer −1, indicating the end of input, which should not be processed. You may assume that 1 <= n <= 50000 and −104 <= xi, yi <= 104 for all i = 1 . . . n.

    Output

    For each test case, print a line containing the maximum area, which contains two digits after the decimal point. You may assume that there is always an answer which is greater than zero.

    Sample Input

    3
    3 4
    2 6
    2 7
    5
    2 6
    3 9
    2 0
    8 0
    6 5
    -1

    Sample Output

    0.50
    27.00

    Source


    选三个点三角形面积最大

    这三个点一定在凸包上

    可以O(n),猜i,j,k单调,然后和旋转卡壳一样枚举i,先让k跑,再让j跑

    事实证明貌似真的单调,discuss里的数据并不能卡掉我的程序....

    注意:跑的时候用面积判断是不是跑到下一个

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    using namespace std;
    typedef long long ll;
    const int N=5e4+5;
    const double eps=1e-8;
    
    inline int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
        return x*f;
    }
    
    inline int sgn(double x){
        if(abs(x)<eps) return 0;
        else return x<0?-1:1;
    }
    
    struct Vector{
        double x,y;
        Vector(double a=0,double b=0):x(a),y(b){}
        bool operator <(const Vector &a)const{
            return sgn(x-a.x)<0||(sgn(x-a.x)==0&&sgn(y-a.y)<0);
        }
    };
    typedef Vector Point;
    Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
    Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
    Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);}
    Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);}
    bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==0&&sgn(a.y-b.y)==0;}
    double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}
    double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}
    
    double Len(Vector a){return sqrt(Dot(a,a));}
    double Len2(Vector a){return Dot(a,a);}
    double DisTL(Point p,Point a,Point b){
        Vector v1=p-a,v2=b-a;
        return abs(Cross(v1,v2)/Len(v2));
    }
    int ConvexHull(Point p[],int n,Point ch[]){
        sort(p+1,p+1+n);
        int m=0;
        for(int i=1;i<=n;i++){
            while(m>1&&sgn(Cross(ch[m]-ch[m-1],p[i]-ch[m-1]))<=0) m--;
            ch[++m]=p[i];
        }
        int k=m;
        for(int i=n-1;i>=1;i--){
            while(m>k&&sgn(Cross(ch[m]-ch[m-1],p[i]-ch[m-1]))<=0) m--;
            ch[++m]=p[i];
        }
        if(n>1) m--;
        return m;
    }
    double RotatingCalipers(Point p[],int n){
        if(n<=2) return 0;
        if(n==3) return abs(Cross(p[3]-p[1],p[2]-p[1]));
        int j=2,k=3;
        double ans=0;
        p[n+1]=p[1];
        for(int i=1;i<=n;i++){
            while(sgn(DisTL(p[k],p[i],p[j])-DisTL(p[k+1],p[i],p[j]))<=0) k=k%n+1;
            //while(sgn(abs(Cross(p[k]-p[i],p[k]-p[j]))-abs(Cross(p[k+1]-p[i],p[k+1]-p[j])))<=0) k=k%n+1;
            ans=max(ans,abs(Cross(p[k]-p[i],p[k]-p[j])));
            //while(sgn(DisTL(p[k],p[i],p[j])-DisTL(p[k],p[i],p[j+1]))<=0) j=j%n+1;
            while(abs(Cross(p[k]-p[i],p[k]-p[j]))-abs(Cross(p[k]-p[i],p[k]-p[j+1]))<=0) j=j%n+1;
            ans=max(ans,abs(Cross(p[k]-p[i],p[k]-p[j])));
        }
        return ans;
    }
    
    int n;
    Point p[N],ch[N];
    int main(int argc, const char * argv[]) {
        while(true){
            n=read();if(n==-1) break;
            for(int i=1;i<=n;i++) p[i].x=read(),p[i].y=read();
            n=ConvexHull(p,n,ch);
            double ans=RotatingCalipers(ch,n);
            printf("%.2f
    ",ans/2);
        }
    }
  • 相关阅读:
    SEO网站优化10大要点
    三维翻动效果的jquery特效代码
    多款国外虚拟主机简单比较
    jquery同步调用ajax
    3D虚拟技术
    最简单jquery.ajax+php例子(对话框显示文本框输入内容),以小见大(初学手记)
    正则表达式学习博客
    关于XHTML头部声明,什么是DOCTYPE?
    Iframe高度自适应(兼容IE/Firefox、同域/跨域)
    3D立体产业链的发展现状和趋势
  • 原文地址:https://www.cnblogs.com/candy99/p/6358406.html
Copyright © 2011-2022 走看看