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);
        }
    }
  • 相关阅读:
    AcWing每日一题--最大的和
    使用 *args 和 **kwargs 的含义
    Python的八大基本数据类型之 元组、列表、字典
    条件判断与if嵌套
    数据拼接与转换
    print()函数与转义字符
    BEGIN-2 序列求和
    BEGIN-1 A+B问题
    并发编程——进程——生产者消费者模型
    并发编程——进程——进程的同步与数据共享
  • 原文地址:https://www.cnblogs.com/candy99/p/6358406.html
Copyright © 2011-2022 走看看